package com.deng.maintenance.dao.impl;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.deng.common.SQLHelper;
import com.deng.common.vo.OrganizationVo;
import com.deng.maintenance.dao.IKPIManager;
import com.deng.maintenance.vo.ItemWeightVo;
import com.deng.maintenance.vo.KpiDetailValueVo;
import com.deng.maintenance.vo.KpiDictItemDetailVo;
import com.deng.maintenance.vo.KpiDictItemVo;
import com.deng.maintenance.vo.KpiDictItemVsOrganizationVo;
import com.deng.maintenance.vo.KpiValueVo;
import com.deng.util.DataFormater;
import com.deng.util.DateTools;
import com.deng.util.MsgReader;

public class KPIManager extends SQLHelper implements IKPIManager {

	private String DICT_KPI_ITEM_COLUMNS = "id,item_name,display_name,description,frequence,"+//id，名称，显示名称，描述，频次（0001，0002，0003，0004）
					"point,target_value,"+//分数，标准值
					"target_type,"+//1越高越好、2越低越好
					"standard_type,"+//0标准、1奖惩、2一票
					"detail_type,"+//0标准、1区间、2明细、3录入
					"different_scale,"+//每偏离？
					"add_type,"+//偏离的话，是增加还是减少  1增2减0等
					"scale_point,"+//偏离一个单位，扣/加的分数
					"formula,"+//整理后的公式
					"default_value," +//默认值
					"section_detail,danwei";
	private String DICT_KPI_ITEM_DETAIL_COLUMNS = "id,parent_id," +
						"item_name,display_name," +
						"target_value,target_type," +
						"different_scale,add_type,scale_point," +
						"no_double,formula,danwei";
	private String KPI_VALUE_COLUMNS = "id,year,month," +
			"kpi_id,kaohe_id,guikou_id,description," +
			"create_time,value,score," +
			"update_score,update_user,update_description,update_time," +
			"valid,valid_user,valid_time";
	
	
	
	
	public List<KpiValueVo> testList(List<KpiValueVo> l)
	{
		return null;
	}
	
	
	public String standardTest(String formula){
		try{
			Connection conn = getConnect();
			ResultSet rs = queryData(conn,"select "+formula);
			String resultS = "";
			while(rs.next()){
				resultS = rs.getBigDecimal(1).setScale(4, BigDecimal.ROUND_HALF_UP).toString();
				break;
			}
			freeConnection(conn);
			return resultS;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}

	public String standardMingxiTest(List formula,List values) {//未完成
//formula:zhibiaomingcheng,xianshimingcheng,description,frequence,fenzhi,arr2
//arr2:zhibiaomingcheng,xianshimingcheng,noDouble,biaozhunzhi,targetType
//differentScale,scalePoint,standardAddType,rtFormula(String)
		try{
//			values = new ArrayList();
//			values.add("5");
//			values.add("15");
//			values.add("25");
//			values.add("50");
			
			String zhibiaomingchengB = (String)formula.get(0);
			String xianshimingchengB = (String)formula.get(1);
			String descriptionB = (String)formula.get(2);
			String frequence = (String)formula.get(3);
			String fenzhi = (String)formula.get(4);	
			List detailArr = (List)formula.get(5);
//			System.out.println("!!!:  "+zhibiaomingchengB+"  "+xianshimingchengB+"  "+detailArr+"   "+detailArr.size());
			String sql = fenzhi;
			for(int i = 0;i<detailArr.size();i++){
				List detailItem = (List)detailArr.get(i);
//				System.out.println(i+"   ---   "+detailItem+"    ");
				sql += " + ";
				String zhibiaomingcheng = (String)detailItem.get(0);
				String xianshimingcheng = (String)detailItem.get(1);
				String noDouble = (String)detailItem.get(2);
				String biaozhunzhi = (String)detailItem.get(3);
				String targetType = (String)detailItem.get(4);
				String differentScale = (String)detailItem.get(5);
				String scalePoint = (String)detailItem.get(6);
				String standardAddType = (String)detailItem.get(7);
				String detailFormula = (String)detailItem.get(8);
				
				String replacedFormula = detailFormula.replaceAll("X", values.get(i)+"");
				
				if(noDouble == "1"){//一次扣分
					sql += "(select case when"+replacedFormula+" < 0 then 0 else "+replacedFormula+" end)";
				}else{//累加扣分
					sql += "(select "+replacedFormula+")";
				}
			}
			System.out.println(sql);
			Connection conn = getConnect();
			ResultSet rs = queryData(conn,"select "+sql);
			String resultS = "";
			while(rs.next()){
				resultS = rs.getBigDecimal(1).setScale(4, BigDecimal.ROUND_HALF_UP).toString();
				break;
			}
			freeConnection(conn);
			return resultS;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}

	
	
	
	
	
	
	
	public void saveDeny(String zhibiaomingchengP, String xianshimingchengP,
			String descriptionP, String frequenceP, String biaozhunzhi,
			String differentScale, String scalePoint, int targetType,
			int addType, int rewardDanwei, String formula, int noDouble) {
		try{
			Connection conn = getConnect();
			int kpi_id = getNewMaxId(conn,"DICT_KPI_ITEM");
			
			String sql = "insert into DICT_KPI_ITEM" +
					"("+DICT_KPI_ITEM_COLUMNS+",no_double) " +//默认值
					" values("+kpi_id+",'"+zhibiaomingchengP+"','"+xianshimingchengP+"','"+descriptionP+"','"+frequenceP+"'," +
					" NULL,"+biaozhunzhi+","+targetType+"," +
					" 2,0," +
					" "+differentScale+","+addType+","+
					" "+scalePoint+",'"+formula+"',"+biaozhunzhi+",NULL,"+rewardDanwei+","+noDouble+")";
			updateData(conn,sql);			
			freeConnection(conn);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public void updateDeny(String zhibiaomingchengP, String xianshimingchengP,
			String descriptionP, String frequenceP, String biaozhunzhi,
			String differentScale, String scalePoint, int targetType,
			int addType, int rewardDanwei, String formula,int kpi_id,int no_double) {
		try{
			Connection conn = getConnect();
			String sql = "update DICT_KPI_ITEM set "+
				" item_name = "+DataFormater.valueToSQL(zhibiaomingchengP)+","+
				" display_name = "+DataFormater.valueToSQL(xianshimingchengP)+","+
				" description = "+DataFormater.valueToSQL(descriptionP)+","+
				" frequence = '"+frequenceP+"',"+
				" point = NULL,"+
				" target_value = "+DataFormater.valueToSQL(biaozhunzhi)+","+
				" target_type = "+DataFormater.valueToSQL(targetType)+","+
				" standard_type = 2,"+
				" detail_type = 0,"+
				" different_scale = "+DataFormater.valueToSQL(differentScale)+","+
				" add_type = "+DataFormater.valueToSQL(addType)+","+
				" scale_point = "+DataFormater.valueToSQL(scalePoint)+","+
				" formula = "+DataFormater.valueToSQL(formula)+","+
				" default_value = "+DataFormater.valueToSQL(biaozhunzhi)+","+
				" section_detail = NULL,"+
				" danwei = "+DataFormater.valueToSQL(rewardDanwei)+","+
				" no_double = "+DataFormater.valueToSQL(no_double)+
				" where id = "+kpi_id;
			
			updateData(conn,sql);			
			freeConnection(conn);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public void updateDeny2(String zhibiaomingchengP, String xianshimingchengP,
			String descriptionP, String frequenceP, String biaozhunzhi,
			String differentScale, String scalePoint, int targetType,
			int addType, int rewardDanwei, String formula,int kaohe_id,int kpi_id) {
		try{
			Connection conn = getConnect();
			String sql = "update KPI_VS_ORGANIZATION set "+
				" item_name = "+DataFormater.valueToSQL(zhibiaomingchengP)+","+
				" display_name = "+DataFormater.valueToSQL(xianshimingchengP)+","+
				" description = "+DataFormater.valueToSQL(descriptionP)+","+
				" frequence = '"+frequenceP+"',"+
				" point = NULL,"+
				" target_value = "+DataFormater.valueToSQL(biaozhunzhi)+","+
				" target_type = "+DataFormater.valueToSQL(targetType)+","+
				" standard_type = 2,"+
				" detail_type = 0,"+
				" different_scale = "+DataFormater.valueToSQL(differentScale)+","+
				" add_type = "+DataFormater.valueToSQL(addType)+","+
				" scale_point = "+DataFormater.valueToSQL(scalePoint)+","+
				" formula = "+DataFormater.valueToSQL(formula)+","+
				" default_value = "+DataFormater.valueToSQL(biaozhunzhi)+","+
				" section_detail = NULL,"+
				" danwei = "+DataFormater.valueToSQL(rewardDanwei)+
				" where kpi_id = "+kpi_id+" and kaohe_id = "+kaohe_id;
			
			updateData(conn,sql);			
			freeConnection(conn);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public void saveDenyInput(String zhibiaomingchengP,
			String xianshimingchengP, String descriptionP, String frequenceP) {
		try{
			Connection conn = getConnect();
			int kpi_id = getNewMaxId(conn,"DICT_KPI_ITEM");
			
			String sql = "insert into DICT_KPI_ITEM" +
					"("+DICT_KPI_ITEM_COLUMNS+") " +//默认值
					" values("+kpi_id+",'"+zhibiaomingchengP+"','"+xianshimingchengP+"','"+descriptionP+"','"+frequenceP+"'," +
					" NULL,NULL,0," +
					" 2,3," +
					" NULL,0,"+
					" NULL,NULL,NULL,NULL,NULL)";
			updateData(conn,sql);			
			freeConnection(conn);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public void updateDenyInput(String zhibiaomingchengP,
			String xianshimingchengP, String descriptionP, String frequenceP,int kpi_id) {
		try{
			Connection conn = getConnect();
			String sql = "update DICT_KPI_ITEM set "+
				" item_name = "+DataFormater.valueToSQL(zhibiaomingchengP)+","+
				" display_name = "+DataFormater.valueToSQL(xianshimingchengP)+","+
				" description = "+DataFormater.valueToSQL(descriptionP)+","+
				" frequence = '"+frequenceP+"',"+
				" point = NULL,"+
				" target_value = NULL,"+
				" target_type = 0,"+
				" standard_type = 2,"+
				" detail_type = 3,"+
				" different_scale = NULL,"+
				" add_type = 0,"+
				" scale_point = NULL,"+
				" formula = NULL,"+
				" default_value = NULL,"+
				" section_detail = NULL,"+
				" danwei = NULL "+
				" where id = "+kpi_id;
		
			
			updateData(conn,sql);			
			freeConnection(conn);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public void updateDenyInput2(String zhibiaomingchengP,
			String xianshimingchengP, String descriptionP, String frequenceP,int kaohe_id,int kpi_id) {
		try{
			Connection conn = getConnect();
			String sql = "update KPI_VS_ORGANIZATION set "+
				" item_name = "+DataFormater.valueToSQL(zhibiaomingchengP)+","+
				" display_name = "+DataFormater.valueToSQL(xianshimingchengP)+","+
				" description = "+DataFormater.valueToSQL(descriptionP)+","+
				" frequence = '"+frequenceP+"',"+
				" point = NULL,"+
				" target_value = NULL,"+
				" target_type = 0,"+
				" standard_type = 2,"+
				" detail_type = 3,"+
				" different_scale = NULL,"+
				" add_type = 0,"+
				" scale_point = NULL,"+
				" formula = NULL,"+
				" default_value = NULL,"+
				" section_detail = NULL,"+
				" danwei = NULL "+
				" where kpi_id = "+kpi_id+" and kaohe_id = "+kaohe_id;
		
			
			updateData(conn,sql);			
			freeConnection(conn);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public void saveReward(String zhibiaomingchengP, String xianshimingchengP,
			String descriptionP, String frequenceP, String biaozhunzhi,
			String differentScale, String scalePoint, int targetType,
			int addType, int rewardDanwei, String formula) {
		try{
			Connection conn = getConnect();
			int kpi_id = getNewMaxId(conn,"DICT_KPI_ITEM");
			
			String sql = "insert into DICT_KPI_ITEM" +
					"("+DICT_KPI_ITEM_COLUMNS+") " +//默认值
					" values("+kpi_id+",'"+zhibiaomingchengP+"','"+xianshimingchengP+"','"+descriptionP+"','"+frequenceP+"'," +
					" 0,"+biaozhunzhi+","+targetType+"," +
					" 1,0," +//0标准、1奖惩、2一票    //0标准、1区间、2明细、3录入
					" "+differentScale+","+addType+","+
					" "+scalePoint+",'"+formula+"',"+biaozhunzhi+",NULL,"+rewardDanwei+")";
			updateData(conn,sql);			
			freeConnection(conn);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public void updateReward(String zhibiaomingchengP, String xianshimingchengP,
			String descriptionP, String frequenceP, String biaozhunzhi,
			String differentScale, String scalePoint, int targetType,
			int addType, int rewardDanwei, String formula,int kpi_id) {
		try{
			Connection conn = getConnect();
			
			String sql = "update DICT_KPI_ITEM set "+
				" item_name = "+DataFormater.valueToSQL(zhibiaomingchengP)+","+
				" display_name = "+DataFormater.valueToSQL(xianshimingchengP)+","+
				" description = "+DataFormater.valueToSQL(descriptionP)+","+
				" frequence = '"+frequenceP+"',"+
				" point = 0,"+
				" target_value = "+DataFormater.valueToSQL(biaozhunzhi)+","+
				" target_type = "+DataFormater.valueToSQL(targetType)+","+
				" standard_type = 1,"+
				" detail_type = 0,"+
				" different_scale = "+DataFormater.valueToSQL(differentScale)+","+
				" add_type = "+DataFormater.valueToSQL(addType)+","+
				" scale_point = "+DataFormater.valueToSQL(scalePoint)+","+
				" formula = "+DataFormater.valueToSQL(formula)+","+
				" default_value = "+DataFormater.valueToSQL(biaozhunzhi)+","+
				" section_detail = NULL,"+
				" danwei = "+DataFormater.valueToSQL(rewardDanwei)+
				" where id = "+kpi_id;
			
			updateData(conn,sql);			
			freeConnection(conn);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public void updateReward2(String zhibiaomingchengP, String xianshimingchengP,
			String descriptionP, String frequenceP, String biaozhunzhi,
			String differentScale, String scalePoint, int targetType,
			int addType, int rewardDanwei, String formula,int kaohe_id,int kpi_id) {
		try{
			Connection conn = getConnect();
			
			String sql = "update KPI_VS_ORGANIZATION set "+
				" item_name = "+DataFormater.valueToSQL(zhibiaomingchengP)+","+
				" display_name = "+DataFormater.valueToSQL(xianshimingchengP)+","+
				" description = "+DataFormater.valueToSQL(descriptionP)+","+
				" frequence = '"+frequenceP+"',"+
				" point = 0,"+
				" target_value = "+DataFormater.valueToSQL(biaozhunzhi)+","+
				" target_type = "+DataFormater.valueToSQL(targetType)+","+
				" standard_type = 1,"+
				" detail_type = 0,"+
				" different_scale = "+DataFormater.valueToSQL(differentScale)+","+
				" add_type = "+DataFormater.valueToSQL(addType)+","+
				" scale_point = "+DataFormater.valueToSQL(scalePoint)+","+
				" formula = "+DataFormater.valueToSQL(formula)+","+
				" default_value = "+DataFormater.valueToSQL(biaozhunzhi)+","+
				" section_detail = NULL,"+
				" danwei = "+DataFormater.valueToSQL(rewardDanwei)+
				" where kpi_id = "+kpi_id+" and kaohe_id = "+kaohe_id;
			
			updateData(conn,sql);			
			freeConnection(conn);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public void saveRewardSection(String zhibiaomingchengP,
			String xianshimingchengP, String descriptionP, String frequenceP,
			String biaozhunzhi, int targetType, List sectionItemList,
			String formula) {
		try{
			String section_detail = "";
			for(int i = 0 ;i<sectionItemList.size();i++){
				Object[] ob = (Object[])sectionItemList.get(i);
//				smallTI.text,bigTI.text,typeCB.selectedItem.value,valueTI.text,danwei
				String small = (String) ob[0];
				String big =(String) ob[1];
				String addType = (String) ob[2];
				String value = (String) ob[3];
				String danwei = (String) ob[4];
				section_detail +=small+","+big+","+addType+","+value+","+danwei+":";
			}
			Connection conn = getConnect();
			int kpi_id = getNewMaxId(conn,"DICT_KPI_ITEM");
			
			String sql = "insert into DICT_KPI_ITEM" +
					"("+DICT_KPI_ITEM_COLUMNS+") " +//section的明细  1,3,4,50:4,1,2,200:4,2,1,800   小，大，加分类型，分数
					" values("+kpi_id+",'"+zhibiaomingchengP+"','"+xianshimingchengP+"','"+descriptionP+"','"+frequenceP+"'," +
					" 0,"+biaozhunzhi+","+targetType+"," +
					" 1,1," +//0标准、1奖惩、2一票     //0标准、1区间、2明细、3录入
					" 0,0,0,"+
					" '"+formula+"',"+biaozhunzhi+",'"+section_detail+"',0)";
			updateData(conn,sql);			
			freeConnection(conn);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public void updateRewardSection(String zhibiaomingchengP,
			String xianshimingchengP, String descriptionP, String frequenceP,
			String biaozhunzhi, int targetType, List sectionItemList,
			String formula,int kpi_id) {
		try{
			String section_detail = "";
			for(int i = 0 ;i<sectionItemList.size();i++){
				Object[] ob = (Object[])sectionItemList.get(i);
//				smallTI.text,bigTI.text,typeCB.selectedItem.value,valueTI.text,danwei
				String small = (String) ob[0];
				String big =(String) ob[1];
				String addType = (String) ob[2];
				String value = (String) ob[3];
				String danwei = (String) ob[4];
				section_detail +=small+","+big+","+addType+","+value+","+danwei+":";
			}
			Connection conn = getConnect();
			String sql = "update DICT_KPI_ITEM set "+
				" item_name = "+DataFormater.valueToSQL(zhibiaomingchengP)+","+
				" display_name = "+DataFormater.valueToSQL(xianshimingchengP)+","+
				" description = "+DataFormater.valueToSQL(descriptionP)+","+
				" frequence = '"+frequenceP+"',"+
				" point = 0,"+
				" target_value = "+DataFormater.valueToSQL(biaozhunzhi)+","+
				" target_type = "+DataFormater.valueToSQL(targetType)+","+
				" standard_type = 1,"+
				" detail_type = 1,"+
				" different_scale = 0,"+
				" add_type = 0,"+
				" scale_point = 0,"+
				" formula = "+DataFormater.valueToSQL(formula)+","+
				" default_value = "+DataFormater.valueToSQL(biaozhunzhi)+","+
				" section_detail =  "+DataFormater.valueToSQL(section_detail)+","+
				" danwei = 0 "+
				" where id = "+kpi_id;
			updateData(conn,sql);			
			freeConnection(conn);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public void updateRewardSection2(String zhibiaomingchengP,
			String xianshimingchengP, String descriptionP, String frequenceP,
			String biaozhunzhi, int targetType, List sectionItemList,
			String formula,int kaohe_id,int kpi_id) {
		try{
			String section_detail = "";
			for(int i = 0 ;i<sectionItemList.size();i++){
				Object[] ob = (Object[])sectionItemList.get(i);
//				smallTI.text,bigTI.text,typeCB.selectedItem.value,valueTI.text,danwei
				String small = (String) ob[0];
				String big =(String) ob[1];
				String addType = (String) ob[2];
				String value = (String) ob[3];
				String danwei = (String) ob[4];
				section_detail +=small+","+big+","+addType+","+value+","+danwei+":";
			}
			Connection conn = getConnect();
			String sql = "update KPI_VS_ORGANIZATION set "+
				" item_name = "+DataFormater.valueToSQL(zhibiaomingchengP)+","+
				" display_name = "+DataFormater.valueToSQL(xianshimingchengP)+","+
				" description = "+DataFormater.valueToSQL(descriptionP)+","+
				" frequence = '"+frequenceP+"',"+
				" point = 0,"+
				" target_value = "+DataFormater.valueToSQL(biaozhunzhi)+","+
				" target_type = "+DataFormater.valueToSQL(targetType)+","+
				" standard_type = 1,"+
				" detail_type = 1,"+
				" different_scale = 0,"+
				" add_type = 0,"+
				" scale_point = 0,"+
				" formula = "+DataFormater.valueToSQL(formula)+","+
				" default_value = "+DataFormater.valueToSQL(biaozhunzhi)+","+
				" section_detail =  "+DataFormater.valueToSQL(section_detail)+","+
				" danwei = 0 "+
				" where kpi_id = "+kpi_id+" and kaohe_id = "+kaohe_id;
			updateData(conn,sql);			
			freeConnection(conn);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public void saveRewardMingxi(String zhibiaomingchengP,
			String xianshimingchengP, String descriptionP, String frequenceP,
			String fenzhiP, List detailOrgArr, List formulaList) {
		try{
			Connection conn = getConnect();
			int kpi_id = getNewMaxId(conn,"DICT_KPI_ITEM");
			
			for(int i = 0 ;i<detailOrgArr.size();i++){
				List ob = (List)detailOrgArr.get(i);
				String zhibiao = (String)ob.get(0);
				String xianshi = (String)ob.get(1);
				String noDouble = (String)ob.get(2);
				String biaozhun = (String)ob.get(3);
				String targetTp = (String)ob.get(4);
				String differentS = (String)ob.get(5);
				String scaleP = (String)ob.get(6);
				String addTp = (String)ob.get(7);
				String danwei = (String)ob.get(8);
				
				List lst = (List)((List)formulaList.get(5)).get(0);
				String formula = (String)(lst).get(8);
				
				
				int cid = getNewMaxId(conn,"DICT_KPI_ITEM_DETAIL");
				
				String csql = "insert into DICT_KPI_ITEM_DETAIL("+DICT_KPI_ITEM_DETAIL_COLUMNS+")" +
						" values("+cid+","+kpi_id+"," +
						" '"+zhibiao+"','"+xianshi+"'," +
						" "+biaozhun+","+targetTp+","+
						" "+differentS+","+addTp+","+scaleP+"," +
						" "+noDouble+",'"+formula+"',"+danwei+")";
				updateData(conn,csql);				
			}
			
			
			String sql = "insert into DICT_KPI_ITEM" +
					"("+DICT_KPI_ITEM_COLUMNS+") " +//section的明细  1,3,4,50:4,1,2,200:4,2,1,800   小，大，加分类型，分数
					" values("+kpi_id+",'"+zhibiaomingchengP+"','"+xianshimingchengP+"','"+descriptionP+"','"+frequenceP+"'," +
					" "+fenzhiP+",0,0," +
					" 1,2," +//0标准、1奖惩、2一票     //0标准、1区间、2明细、3录入
					" 0,0,0,"+
					" NULL,NULL,NULL,0)";
			updateData(conn,sql);			
			freeConnection(conn);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}

	public void updateRewardMingxi(String zhibiaomingchengP,
			String xianshimingchengP, String descriptionP, String frequenceP,
			String fenzhiP, List detailOrgArr, List formulaList,int kpi_id) {
		try{
			Connection conn = getConnect();
			
			for(int i = 0 ;i<detailOrgArr.size();i++){
				List ob = (List)detailOrgArr.get(i);
				String zhibiao = (String)ob.get(0);
				String xianshi = (String)ob.get(1);
				String noDouble = (String)ob.get(2);
				String biaozhun = (String)ob.get(3);
				String targetTp = (String)ob.get(4);
				String differentS = (String)ob.get(5);
				String scaleP = (String)ob.get(6);
				String addTp = (String)ob.get(7);
				String danwei = (String)ob.get(8);
				String mingxi_id = (String)ob.get(9);
				
				List lst = (List)((List)formulaList.get(5)).get(0);
				String formula = (String)(lst).get(8);
				
				
				
				String csql  = "";
				if(mingxi_id.equals("0"))
				{
					int cid = getNewMaxId(conn,"DICT_KPI_ITEM_DETAIL");
					csql = "insert into DICT_KPI_ITEM_DETAIL("+DICT_KPI_ITEM_DETAIL_COLUMNS+")" +
						" values("+cid+","+kpi_id+"," +
						" '"+zhibiao+"','"+xianshi+"'," +
						" "+biaozhun+","+targetTp+","+
						" "+differentS+","+addTp+","+scaleP+"," +
						" "+noDouble+",'"+formula+"',"+danwei+")";
				}
				else
				{
					csql = "update DICT_KPI_ITEM_DETAIL " +
					"set parent_id = "+DataFormater.valueToSQL(kpi_id)+","+
					" item_name = "+DataFormater.valueToSQL(zhibiao)+","+
					" display_name = "+DataFormater.valueToSQL(xianshi)+","+
					" target_value = "+DataFormater.valueToSQL(biaozhun)+","+
					" target_type = "+DataFormater.valueToSQL(targetTp)+","+
					" different_scale = "+DataFormater.valueToSQL(differentS)+","+
					" add_type = "+DataFormater.valueToSQL(addTp)+","+
					" scale_point = "+DataFormater.valueToSQL(scaleP)+","+
					" no_double = "+DataFormater.valueToSQL(noDouble)+","+
					" formula = "+DataFormater.valueToSQL(formula)+","+
					" danwei = "+DataFormater.valueToSQL(danwei)+" "+
					" where id = "+mingxi_id;
					
				}
				updateData(conn,csql);				
			}
			
			String sql = "update DICT_KPI_ITEM set "+
				" item_name = "+DataFormater.valueToSQL(zhibiaomingchengP)+","+
				" display_name = "+DataFormater.valueToSQL(xianshimingchengP)+","+
				" description = "+DataFormater.valueToSQL(descriptionP)+","+
				" frequence = '"+frequenceP+"',"+
				" point = "+DataFormater.valueToSQL(fenzhiP)+","+
				" target_value = 0,"+
				" target_type = 0,"+
				" standard_type = 1,"+
				" detail_type = 2,"+
				" different_scale = 0,"+
				" add_type = 0,"+
				" scale_point = 0,"+
				" formula = NULL,"+
				" default_value = NULL,"+
				" section_detail =  NULL,"+
				" danwei = 0 "+
				" where id = "+kpi_id;
			
			updateData(conn,sql);			
			freeConnection(conn);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public void updateRewardMingxi2(String zhibiaomingchengP,
			String xianshimingchengP, String descriptionP, String frequenceP,
			String fenzhiP, List detailOrgArr, List formulaList,int kaohe_id,int kpi_id) {
		try{
			Connection conn = getConnect();
			
			for(int i = 0 ;i<detailOrgArr.size();i++){
				List ob = (List)detailOrgArr.get(i);
				String zhibiao = (String)ob.get(0);
				String xianshi = (String)ob.get(1);
				String noDouble = (String)ob.get(2);
				String biaozhun = (String)ob.get(3);
				String targetTp = (String)ob.get(4);
				String differentS = (String)ob.get(5);
				String scaleP = (String)ob.get(6);
				String addTp = (String)ob.get(7);
				String danwei = (String)ob.get(8);
				String mingxi_id = (String)ob.get(9);
				
				List lst = (List)((List)formulaList.get(5)).get(0);
				String formula = (String)(lst).get(8);
				
				
				
				String csql = "update KPI_DETAIL_VS_ORGANIZATION " +
					" set parent_id = "+DataFormater.valueToSQL(kpi_id)+","+
					" item_name = "+DataFormater.valueToSQL(zhibiao)+","+
					" display_name = "+DataFormater.valueToSQL(xianshi)+","+
					" target_value = "+DataFormater.valueToSQL(biaozhun)+","+
					" target_type = "+DataFormater.valueToSQL(targetTp)+","+
					" different_scale = "+DataFormater.valueToSQL(differentS)+","+
					" add_type = "+DataFormater.valueToSQL(addTp)+","+
					" scale_point = "+DataFormater.valueToSQL(scaleP)+","+
					" no_double = "+DataFormater.valueToSQL(noDouble)+","+
					" formula = "+DataFormater.valueToSQL(formula)+","+
					" danwei = "+DataFormater.valueToSQL(danwei)+" "+
					" where kpi_detail_id = "+mingxi_id+" and kaohe_id = "+kaohe_id;
				updateData(conn,csql);				
			}
			
			String sql = "update KPI_VS_ORGANIZATION set "+
				" item_name = "+DataFormater.valueToSQL(zhibiaomingchengP)+","+
				" display_name = "+DataFormater.valueToSQL(xianshimingchengP)+","+
				" description = "+DataFormater.valueToSQL(descriptionP)+","+
				" frequence = '"+frequenceP+"',"+
				" point = "+DataFormater.valueToSQL(fenzhiP)+","+
				" target_value = 0,"+
				" target_type = 0,"+
				" standard_type = 1,"+
				" detail_type = 2,"+
				" different_scale = 0,"+
				" add_type = 0,"+
				" scale_point = 0,"+
				" formula = NULL,"+
				" default_value = NULL,"+
				" section_detail =  NULL,"+
				" danwei = 0 "+
				" where kpi_id = "+kpi_id+" and kaohe_id = "+kaohe_id;
			
			updateData(conn,sql);			
			freeConnection(conn);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}

	public void saveStandard(String zhibiaomingchengP,
			String xianshimingchengP, String descriptionP, String frequenceP,
			String fenzhi, String biaozhunzhi, String differentScale,
			String scalePoint, int targetType, int addType, String formula) {
		try{
			Connection conn = getConnect();
			int kpi_id = getNewMaxId(conn,"DICT_KPI_ITEM");
			String sql = "insert into DICT_KPI_ITEM" +
					"("+DICT_KPI_ITEM_COLUMNS+") " +//默认值
					" values("+kpi_id+",'"+zhibiaomingchengP+"','"+xianshimingchengP+"','"+descriptionP+"','"+frequenceP+"'," +
					" "+fenzhi+","+biaozhunzhi+","+targetType+"," +
					" 0,0," +
					" "+differentScale+","+addType+","+
					" "+scalePoint+",'"+formula+"',"+biaozhunzhi+",NULL,0)";
			updateData(conn,sql);			
			freeConnection(conn);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public void updateStandard(String zhibiaomingchengP,
			String xianshimingchengP, String descriptionP, String frequenceP,
			String fenzhi, String biaozhunzhi, String differentScale,
			String scalePoint, int targetType, int addType, String formula,int kpi_id) {
		try{
			Connection conn = getConnect();
			String sql = "update DICT_KPI_ITEM set "+
					" item_name = "+DataFormater.valueToSQL(zhibiaomingchengP)+","+
					" display_name = "+DataFormater.valueToSQL(xianshimingchengP)+","+
					" description = "+DataFormater.valueToSQL(descriptionP)+","+
					" frequence = '"+frequenceP+"',"+
					" point = "+DataFormater.valueToSQL(fenzhi)+","+
					" target_value = "+DataFormater.valueToSQL(biaozhunzhi)+","+
					" target_type = "+DataFormater.valueToSQL(targetType)+","+
					" standard_type = 0,"+
					" detail_type = 0,"+
					" different_scale = "+DataFormater.valueToSQL(differentScale)+","+
					" add_type = "+DataFormater.valueToSQL(addType)+","+
					" scale_point = "+DataFormater.valueToSQL(scalePoint)+","+
					" formula = "+DataFormater.valueToSQL(formula)+","+
					" default_value = "+DataFormater.valueToSQL(biaozhunzhi)+","+
					" section_detail = NULL,"+
					" danwei = 0 "+
					" where id = "+kpi_id;
			updateData(conn,sql);			
			freeConnection(conn);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public void updateStandard2(String zhibiaomingchengP,
			String xianshimingchengP, String descriptionP, String frequenceP,
			String fenzhi, String biaozhunzhi, String differentScale,
			String scalePoint, int targetType, int addType, String formula,int kaohe_id,int kpi_id) {
		try{
			Connection conn = getConnect();
			String sql = "update KPI_VS_ORGANIZATION set "+
					" item_name = "+DataFormater.valueToSQL(zhibiaomingchengP)+","+
					" display_name = "+DataFormater.valueToSQL(xianshimingchengP)+","+
					" description = "+DataFormater.valueToSQL(descriptionP)+","+
					" frequence = '"+frequenceP+"',"+
					" point = "+DataFormater.valueToSQL(fenzhi)+","+
					" target_value = "+DataFormater.valueToSQL(biaozhunzhi)+","+
					" target_type = "+DataFormater.valueToSQL(targetType)+","+
					" standard_type = 0,"+
					" detail_type = 0,"+
					" different_scale = "+DataFormater.valueToSQL(differentScale)+","+
					" add_type = "+DataFormater.valueToSQL(addType)+","+
					" scale_point = "+DataFormater.valueToSQL(scalePoint)+","+
					" formula = "+DataFormater.valueToSQL(formula)+","+
					" default_value = "+DataFormater.valueToSQL(biaozhunzhi)+","+
					" section_detail = NULL,"+
					" danwei = 0 "+
					" where kpi_id = "+kpi_id+" and kaohe_id = "+kaohe_id;
			updateData(conn,sql);			
			freeConnection(conn);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public void saveStandardSection(String zhibiaomingchengP,
			String xianshimingchengP, String descriptionP, String frequenceP,
			String fenzhi, String biaozhunzhi, int targetType,
			List sectionItemList, String formula) {
		try{
			String section_detail = "";
			for(int i = 0 ;i<sectionItemList.size();i++){
				Object[] ob = (Object[])sectionItemList.get(i);
//				smallTI.text,bigTI.text,typeCB.selectedItem.value,valueTI.text
				String small = (String) ob[0];
				String big =(String) ob[1];
				String addType = (String) ob[2];
				String value = (String) ob[3];
				section_detail +=small+","+big+","+addType+","+value+":";
			}
			Connection conn = getConnect();
			int kpi_id = getNewMaxId(conn,"DICT_KPI_ITEM");
			String sql = "insert into DICT_KPI_ITEM" +
					"("+DICT_KPI_ITEM_COLUMNS+") " +//section的明细  1,3,4,50:4,1,2,200:4,2,1,800   小，大，加分类型，分数
					" values("+kpi_id+",'"+zhibiaomingchengP+"','"+xianshimingchengP+"','"+descriptionP+"','"+frequenceP+"'," +
					" "+fenzhi+","+biaozhunzhi+","+targetType+"," +
					" 0,1," +//0标准、1奖惩、2一票     //0标准、1区间、2明细、3录入
					" 0,0,0,"+
					" '"+formula+"',"+biaozhunzhi+",'"+section_detail+"',0)";
			updateData(conn,sql);			
			freeConnection(conn);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public void updateStandardSection(String zhibiaomingchengP,
			String xianshimingchengP, String descriptionP, String frequenceP,
			String fenzhi, String biaozhunzhi, int targetType,
			List sectionItemList, String formula,int kpi_id) {
		try{
			String section_detail = "";
			for(int i = 0 ;i<sectionItemList.size();i++){
				Object[] ob = (Object[])sectionItemList.get(i);
//				smallTI.text,bigTI.text,typeCB.selectedItem.value,valueTI.text
				String small = (String) ob[0];
				String big =(String) ob[1];
				String addType = (String) ob[2];
				String value = (String) ob[3];
				section_detail +=small+","+big+","+addType+","+value+":";
			}
			Connection conn = getConnect();
			String sql = "update DICT_KPI_ITEM set "+
					" item_name = "+DataFormater.valueToSQL(zhibiaomingchengP)+","+
					" display_name = "+DataFormater.valueToSQL(xianshimingchengP)+","+
					" description = "+DataFormater.valueToSQL(descriptionP)+","+
					" frequence = '"+frequenceP+"',"+
					" point = "+DataFormater.valueToSQL(fenzhi)+","+
					" target_value = "+DataFormater.valueToSQL(biaozhunzhi)+","+
					" target_type = "+DataFormater.valueToSQL(targetType)+","+
					" standard_type = 0,"+
					" detail_type = 1,"+
					" different_scale = 0,"+
					" add_type = 0,"+
					" scale_point = 0,"+
					" formula = "+DataFormater.valueToSQL(formula)+","+
					" default_value = "+DataFormater.valueToSQL(biaozhunzhi)+","+
					" section_detail =  "+DataFormater.valueToSQL(section_detail)+","+
					" danwei = 0 "+
					" where id = "+kpi_id;
			updateData(conn,sql);			
			freeConnection(conn);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public void updateStandardSection2(String zhibiaomingchengP,
			String xianshimingchengP, String descriptionP, String frequenceP,
			String fenzhi, String biaozhunzhi, int targetType,
			List sectionItemList, String formula,int kaohe_id,int kpi_id) {
		try{
			String section_detail = "";
			for(int i = 0 ;i<sectionItemList.size();i++){
				Object[] ob = (Object[])sectionItemList.get(i);
//				smallTI.text,bigTI.text,typeCB.selectedItem.value,valueTI.text
				String small = (String) ob[0];
				String big =(String) ob[1];
				String addType = (String) ob[2];
				String value = (String) ob[3];
				section_detail +=small+","+big+","+addType+","+value+":";
			}
			Connection conn = getConnect();
			String sql = "update KPI_VS_ORGANIZATION set "+
					" item_name = "+DataFormater.valueToSQL(zhibiaomingchengP)+","+
					" display_name = "+DataFormater.valueToSQL(xianshimingchengP)+","+
					" description = "+DataFormater.valueToSQL(descriptionP)+","+
					" frequence = '"+frequenceP+"',"+
					" point = "+DataFormater.valueToSQL(fenzhi)+","+
					" target_value = "+DataFormater.valueToSQL(biaozhunzhi)+","+
					" target_type = "+DataFormater.valueToSQL(targetType)+","+
					" standard_type = 0,"+
					" detail_type = 1,"+
					" different_scale = 0,"+
					" add_type = 0,"+
					" scale_point = 0,"+
					" formula = "+DataFormater.valueToSQL(formula)+","+
					" default_value = "+DataFormater.valueToSQL(biaozhunzhi)+","+
					" section_detail =  "+DataFormater.valueToSQL(section_detail)+","+
					" danwei = 0 "+
					" where kpi_id = "+kpi_id+" and kaohe_id = "+kaohe_id;
			updateData(conn,sql);			
			freeConnection(conn);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public void saveStandardMingxi(String zhibiaomingchengP,
			String xianshimingchengP, String descriptionP, String frequenceP,
			String fenzhiP, List detailOrgArr, List formulaList) {
		try{
			Connection conn = getConnect();
			int kpi_id = getNewMaxId(conn,"DICT_KPI_ITEM");
			for(int i = 0 ;i<detailOrgArr.size();i++){
				List ob = (List)detailOrgArr.get(i);
				String zhibiao = (String)ob.get(0);
				String xianshi = (String)ob.get(1);
				String noDouble = (String)ob.get(2);
				String biaozhun = (String)ob.get(3);
				String targetTp = (String)ob.get(4);
				String differentS = (String)ob.get(5);
				String scaleP = (String)ob.get(6);
				String addTp = (String)ob.get(7);
				List lst = (List)((List)formulaList.get(5)).get(i);
				String formula = (String)(lst).get(8);
				String danwei = (String)(lst).get(9);
				
				int cid = getNewMaxId(conn,"DICT_KPI_ITEM_DETAIL");
				
				String csql = "insert into DICT_KPI_ITEM_DETAIL("+DICT_KPI_ITEM_DETAIL_COLUMNS+")" +
						" values("+cid+","+kpi_id+"," +
						" '"+zhibiao+"','"+xianshi+"'," +
						" "+biaozhun+","+targetTp+","+
						" "+differentS+","+addTp+","+scaleP+"," +
						" "+noDouble+",'"+formula+"',0)";
				updateData(conn,csql);				
			}
						
			String sql = "insert into DICT_KPI_ITEM" +
					"("+DICT_KPI_ITEM_COLUMNS+") " +//section的明细  1,3,4,50:4,1,2,200:4,2,1,800   小，大，加分类型，分数
					" values("+kpi_id+",'"+zhibiaomingchengP+"','"+xianshimingchengP+"','"+descriptionP+"','"+frequenceP+"'," +
					" "+fenzhiP+",0,0," +
					" 0,2," +//0标准、1奖惩、2一票     //0标准、1区间、2明细、3录入
					" 0,0,0,"+
					" NULL,NULL,NULL,0)";
			updateData(conn,sql);			
			freeConnection(conn);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public void updateStandardMingxi(String zhibiaomingchengP,
			String xianshimingchengP, String descriptionP, String frequenceP,
			String fenzhiP, List detailOrgArr, List formulaList,int kpi_id) {
		try{
			Connection conn = getConnect();
			for(int i = 0 ;i<detailOrgArr.size();i++){
				List ob = (List)detailOrgArr.get(i);
				String zhibiao = (String)ob.get(0);
				String xianshi = (String)ob.get(1);
				String noDouble = (String)ob.get(2);
				String biaozhun = (String)ob.get(3);
				String targetTp = (String)ob.get(4);
				String differentS = (String)ob.get(5);
				String scaleP = (String)ob.get(6);
				String addTp = (String)ob.get(7);
				String mingxi_id = (String)ob.get(8);
				
				List lst = (List)((List)formulaList.get(5)).get(i);
				String formula = (String)(lst).get(8);
				String danwei = (String)(lst).get(9);
				
				
				String csql = "";
				if(mingxi_id.equals("0"))
				{
					int cid = getNewMaxId(conn,"DICT_KPI_ITEM_DETAIL");
					csql = "insert into DICT_KPI_ITEM_DETAIL("+DICT_KPI_ITEM_DETAIL_COLUMNS+")" +
						" values("+cid+","+kpi_id+"," +
						" '"+zhibiao+"','"+xianshi+"'," +
						" "+biaozhun+","+targetTp+","+
						" "+differentS+","+addTp+","+scaleP+"," +
						" "+noDouble+",'"+formula+"',0)";
				}
				else
				{					
					csql = "update DICT_KPI_ITEM_DETAIL " +
							"set parent_id = "+DataFormater.valueToSQL(kpi_id)+","+
							" item_name = "+DataFormater.valueToSQL(zhibiao)+","+
							" display_name = "+DataFormater.valueToSQL(xianshi)+","+
							" target_value = "+DataFormater.valueToSQL(biaozhun)+","+
							" target_type = "+DataFormater.valueToSQL(targetTp)+","+
							" different_scale = "+DataFormater.valueToSQL(differentS)+","+
							" add_type = "+DataFormater.valueToSQL(addTp)+","+
							" scale_point = "+DataFormater.valueToSQL(scaleP)+","+
							" no_double = "+DataFormater.valueToSQL(noDouble)+","+
							" formula = "+DataFormater.valueToSQL(formula)+","+
							" danwei = 0 "+
							" where id = "+mingxi_id;
				}				
				updateData(conn,csql);				
			}
			String sql = "update DICT_KPI_ITEM set "+
				" item_name = "+DataFormater.valueToSQL(zhibiaomingchengP)+","+
				" display_name = "+DataFormater.valueToSQL(xianshimingchengP)+","+
				" description = "+DataFormater.valueToSQL(descriptionP)+","+
				" frequence = '"+frequenceP+"',"+
				" point = "+DataFormater.valueToSQL(fenzhiP)+","+
				" target_value = 0,"+
				" target_type = 0,"+
				" standard_type = 0,"+
				" detail_type = 2,"+
				" different_scale = 0,"+
				" add_type = 0,"+
				" scale_point = 0,"+
				" formula = NULL,"+
				" default_value = NULL,"+
				" section_detail =  NULL,"+
				" danwei = 0 "+
				" where id = "+kpi_id;
			
			updateData(conn,sql);			
			freeConnection(conn);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public void updateStandardMingxi2(String zhibiaomingchengP,
			String xianshimingchengP, String descriptionP, String frequenceP,
			String fenzhiP, List detailOrgArr, List formulaList,int kaohe_id,int kpi_id) {
		try{
			Connection conn = getConnect();
			for(int i = 0 ;i<detailOrgArr.size();i++){
				List ob = (List)detailOrgArr.get(i);
				String zhibiao = (String)ob.get(0);
				String xianshi = (String)ob.get(1);
				String noDouble = (String)ob.get(2);
				String biaozhun = (String)ob.get(3);
				String targetTp = (String)ob.get(4);
				String differentS = (String)ob.get(5);
				String scaleP = (String)ob.get(6);
				String addTp = (String)ob.get(7);
				String mingxi_id = (String)ob.get(8);
				
				List lst = (List)((List)formulaList.get(5)).get(i);
				String formula = (String)(lst).get(8);
				String danwei = (String)(lst).get(9);
				
				
				String csql = "update KPI_DETAIL_VS_ORGANIZATION " +
					"set parent_id = "+DataFormater.valueToSQL(kpi_id)+","+
					" item_name = "+DataFormater.valueToSQL(zhibiao)+","+
					" display_name = "+DataFormater.valueToSQL(xianshi)+","+
					" target_value = "+DataFormater.valueToSQL(biaozhun)+","+
					" target_type = "+DataFormater.valueToSQL(targetTp)+","+
					" different_scale = "+DataFormater.valueToSQL(differentS)+","+
					" add_type = "+DataFormater.valueToSQL(addTp)+","+
					" scale_point = "+DataFormater.valueToSQL(scaleP)+","+
					" no_double = "+DataFormater.valueToSQL(noDouble)+","+
					" formula = "+DataFormater.valueToSQL(formula)+","+
					" danwei = 0 "+
					" where kpi_detail_id = "+mingxi_id+" and kaohe_id = "+kaohe_id;			
				updateData(conn,csql);				
			}
			String sql = "update KPI_VS_ORGANIZATION set "+
				" item_name = "+DataFormater.valueToSQL(zhibiaomingchengP)+","+
				" display_name = "+DataFormater.valueToSQL(xianshimingchengP)+","+
				" description = "+DataFormater.valueToSQL(descriptionP)+","+
				" frequence = '"+frequenceP+"',"+
				" point = "+DataFormater.valueToSQL(fenzhiP)+","+
				" target_value = 0,"+
				" target_type = 0,"+
				" standard_type = 0,"+
				" detail_type = 2,"+
				" different_scale = 0,"+
				" add_type = 0,"+
				" scale_point = 0,"+
				" formula = NULL,"+
				" default_value = NULL,"+
				" section_detail =  NULL,"+
				" danwei = 0 "+
				" where kpi_id = "+kpi_id+" and kaohe_id = "+kaohe_id;
			
			updateData(conn,sql);			
			freeConnection(conn);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public List<KpiDictItemVo> getAllKpiDictItem(String includeName)
	{
		try{
			Connection conn = getConnect();
			String sql = "";
			if(includeName.trim().equals("")){
				sql = "select p.id,p.item_name,p.display_name,p.description,p.frequence,p.point," +
						" p.target_value,p.target_type," +
						" p.standard_type,p.detail_type,p.different_scale,p.add_type,p.scale_point," +
						" p.formula,p.default_value,p.section_detail,p.danwei " +
						" from DICT_KPI_ITEM as p" +
						" order by p.id";
			}else{
				sql = "select p.id,p.item_name,p.display_name,p.description,p.frequence,p.point," +
				" p.target_value,p.target_type," +
				" p.standard_type,p.detail_type,p.different_scale,p.add_type,p.scale_point," +
				" p.formula,p.default_value,p.section_detail,p.danwei " +
				" from DICT_KPI_ITEM as p" +
				" where p.item_name like '%"+includeName+"%'"+
				" order by p.id";				
			}
			
			ResultSet rs = queryData(conn,sql);
			List<KpiDictItemVo> rtList = new ArrayList();
			KpiDictItemVo vo ;
			while(rs.next()){
//				"id,item_name,display_name,description,frequence,"+//id，名称，显示名称，描述，频次（0001，0002，0003，0004）
//				"point,target_value,"+//分数，标准值
//				"target_type,"+//1越高越好、2越低越好
//				"standard_type,"+//0标准、1奖惩、2一票
//				"detail_type,"+//0标准、1区间、2明细、3录入
//				"different_scale,"+//每偏离？
//				"add_type,"+//偏离的话，是增加还是减少  1增2减0等
//				"scale_point,"+//偏离一个单位，扣/加的分数
//				"formula,"+//整理后的公式
//				"default_value," +//默认值
//				"section_detail,danwei
				vo = new KpiDictItemVo();
				vo.id = rs.getInt("id");
				vo.item_name = rs.getString("item_name");
				vo.display_name = rs.getString("display_name");
				vo.description = rs.getString("description");
				vo.frequence = rs.getString("frequence");
				vo.point = DataFormater.numberToString(rs.getBigDecimal("point"));
				vo.target_value = DataFormater.numberToString(rs.getBigDecimal("target_value"));
				vo.target_type = rs.getInt("target_type");
				vo.standard_type = rs.getInt("standard_type");
				vo.detail_type = rs.getInt("detail_type");
				vo.different_scale = DataFormater.numberToString(rs.getBigDecimal("different_scale"));
				vo.add_type = rs.getInt("add_type");
				vo.scale_point = DataFormater.numberToString(rs.getBigDecimal("scale_point"));
				vo.formula = rs.getString("formula");
				vo.default_value = DataFormater.numberToString(rs.getBigDecimal("default_value"));
				vo.section_detail = rs.getString("section_detail");
				vo.danwei = rs.getInt("danwei");
				
				rtList.add(vo);
			}
			freeConnection(conn);
			return rtList;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	public List<KpiDictItemVo> getAllKpiDictItemWithDetail(String includeName) {
		try{
			Connection conn = getConnect();
			String sql = "";
			if(includeName.trim().equals("")){
				sql = "select p.id,p.item_name,p.display_name,p.description,p.frequence,p.point," +
						" p.target_value,p.target_type," +
						" p.standard_type,p.detail_type,p.different_scale,p.add_type,p.scale_point," +
						" p.formula,p.default_value,p.section_detail,p.danwei," +
						" c.id as c_id,c.item_name as c_item_name,c.display_name as c_display_name," +
						" c.target_value as c_target_value,c.target_type as c_target_type," +
						" c.different_scale as c_different_scale,c.add_type as c_add_type," +
						" c.scale_point as c_scale_point,c.no_double as c_no_double,c.formula as c_formula," +
						" c.danwei as c_danwei" +
						" from DICT_KPI_ITEM as p" +
						" left join DICT_KPI_ITEM_DETAIL as c" +
						" on p.id = c.parent_id" +
						" order by p.id,c.id";//p.standard_type,p.detail_type,p.id,c.id";
			}else{
				sql = "select p.id,p.item_name,p.display_name,p.description,p.frequence,p.point," +
				" p.target_value,p.target_type," +
				" p.standard_type,p.detail_type,p.different_scale,p.add_type,p.scale_point," +
				" p.formula,p.default_value,p.section_detail,p.danwei," +
				" c.id as c_id,c.item_name as c_item_name,c.display_name as c_display_name," +
				" c.target_value as c_target_value,c.target_type as c_target_type," +
				" c.different_scale as c_different_scale,c.add_type as c_add_type," +
				" c.scale_point as c_scale_point,c.no_double as c_no_double,c.formula as c_formula," +
				" c.danwei as c_danwei" +
				" from DICT_KPI_ITEM as p" +
				" left join DICT_KPI_ITEM_DETAIL as c" +
				" on p.id = c.parent_id" +
				" where p.item_name like '%"+includeName+"%'"+
				" order by p.id,c.id";//p.standard_type,p.detail_type,p.id,c.id";				
			}
			
			ResultSet rs = queryData(conn,sql);
			List<KpiDictItemVo> rtList = new ArrayList();
//			List<Object[]> cList  = new ArrayList();//p_id,KpiDictItemDetailVo
			KpiDictItemVo vo ;
			while(rs.next()){
//				"id,item_name,display_name,description,frequence,"+//id，名称，显示名称，描述，频次（0001，0002，0003，0004）
//				"point,target_value,"+//分数，标准值
//				"target_type,"+//1越高越好、2越低越好
//				"standard_type,"+//0标准、1奖惩、2一票
//				"detail_type,"+//0标准、1区间、2明细、3录入
//				"different_scale,"+//每偏离？
//				"add_type,"+//偏离的话，是增加还是减少  1增2减0等
//				"scale_point,"+//偏离一个单位，扣/加的分数
//				"formula,"+//整理后的公式
//				"default_value," +//默认值
//				"section_detail,danwei
				vo = new KpiDictItemVo();
				vo.id = rs.getInt("id");
				vo.item_name = rs.getString("item_name");
				vo.display_name = rs.getString("display_name");
				vo.description = rs.getString("description");
				vo.frequence = rs.getString("frequence");
				vo.point = DataFormater.numberToString(rs.getBigDecimal("point"));
				vo.target_value = DataFormater.numberToString(rs.getBigDecimal("target_value"));
				vo.target_type = rs.getInt("target_type");
				vo.standard_type = rs.getInt("standard_type");
				vo.detail_type = rs.getInt("detail_type");
				vo.different_scale = DataFormater.numberToString(rs.getBigDecimal("different_scale"));
				vo.add_type = rs.getInt("add_type");
				vo.scale_point = DataFormater.numberToString(rs.getBigDecimal("scale_point"));
				vo.formula = rs.getString("formula");
				vo.default_value = DataFormater.numberToString(rs.getBigDecimal("default_value"));
				vo.section_detail = rs.getString("section_detail");
				vo.danwei = rs.getInt("danwei");
//				vo.c_id = rs.getInt("c_id");
				if(vo.detail_type == 2)//有明细
				{
//					c.id as c_id,c.item_name as c_item_name,c.display_name as c_display_name,
//					c.target_value as c_target_value,c.target_type as c_target_type,
//					c.different_scale as c_different_scale,c.add_type as c_add_type,
//					c.scale_point as c_scale_point,c.no_double as c_no_double,c.formula as c_formula,c.danwei as c_danwei
					KpiDictItemDetailVo cvo = new KpiDictItemDetailVo();
					cvo.id = rs.getInt("c_id");
					cvo.item_name = rs.getString("c_item_name");
					cvo.display_name = rs.getString("c_display_name");
					cvo.target_value = DataFormater.numberToString(rs.getBigDecimal("c_target_value"));
					cvo.target_type = rs.getInt("c_target_type");
					cvo.different_scale = DataFormater.numberToString(rs.getBigDecimal("c_different_scale"));
					cvo.add_type = rs.getInt("c_add_type");
					cvo.scale_point = DataFormater.numberToString(rs.getBigDecimal("c_scale_point"));
					cvo.no_double = rs.getInt("c_no_double");
					cvo.formula = rs.getString("c_formula");
					cvo.danwei = rs.getInt("c_danwei");
					
					vo.detail_vo = cvo;
//					Object[] ob = new Object[]{vo.id,cvo};
//					cList.add(ob);
				}				
				rtList.add(vo);
			}
//			for(KpiDictItemVo pvo:rtList)
//			{
//				if(pvo.detail_type == 2)
//				{
//					int id = pvo.id;
//					for(Object[] ob:cList)
//					{
//						if((Integer)ob[0] == id)
//						{
//							pvo.
//						}
//					}
//				}
//			}
			freeConnection(conn);
			return rtList;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	public List<KpiDictItemVo> getAllKpiDictItemForDetailShow(String includeName)
	{
		List<KpiDictItemVo> list = getAllKpiDictItemWithDetail(includeName);
		try
		{			
			int tmpId = 0;
			for(int i = 0;i< list.size();i++)
			{
				KpiDictItemVo vo = list.get(i); 
				if(vo.detail_type == 2)
				{
					if(vo.id == tmpId)
					{
						continue;
					}
					KpiDictItemVo newVo = new KpiDictItemVo();
					newVo.id	=	vo.id;
					newVo.item_name	=	vo.item_name;
					newVo.display_name	=	vo.display_name;
					newVo.description	=	vo.description;
					newVo.frequence	=	vo.frequence;
					newVo.point	=	vo.point;
					newVo.target_value	=	vo.target_value;
					newVo.target_type	=	vo.target_type;
					newVo.standard_type	=	vo.standard_type;
					newVo.detail_type	=	vo.detail_type;
					newVo.different_scale	=	vo.different_scale;
					newVo.add_type	=	vo.add_type;
					newVo.scale_point	=	vo.scale_point;
					newVo.formula	=	vo.formula;
					newVo.default_value	=	vo.default_value;
					newVo.section_detail	=	vo.section_detail;
					newVo.danwei	=	vo.danwei;
					tmpId = vo.id;
					list.add(i, newVo);
					
				}
//				if(vo.standard_type == 0)
//				{
//					if(vo.detail_type == 1)
//					{	
//						String rtS = "";
//						String s[] = vo.section_detail.split(":");
//						for(int t = 0; t < s.length ; t++)
//						{
//							String s2[] = s[t].split(",");
//							String eq = "";
//							if(s2[2].equals("1"))
//							{
//								eq = "增加";
//							}
//							else if(s2[2].equals("2"))
//							{
//								eq = "减少";
//							}
//							else
//							{
//								eq = "等于";
//							}
//							rtS += "当值处于"+s2[0]+"-"+s2[1]+"之间,"+eq+s2[3]+"分;";
//						}
//						System.out.println( rtS);
//					}
//					else if(vo.detail_type == 2)
//					{
//						if(vo.detail_vo == null)
//						{
//							
//						}
//						else
//						{
//							System.out.println( vo.detail_vo.display_name);
//						}
//					}
//				}
//				else if(vo.standard_type == 1)
//				{
//					if(vo.detail_type == 1)
//					{	
//						String rtS = "";
//						String s[] = vo.section_detail.split(":");
//						for(int t = 0; t < s.length ; t++)
//						{
//							String s2[] = s[t].split(",");
//							String eq = "";
//							String danwei = "";
//							if(s2[2].equals("1"))
//							{
//								eq = "奖励";
//							}
//							else if(s2[2].equals("2"))
//							{
//								eq = "扣除";
//							}
//							else
//							{
//								eq = " ";
//							}
//							if(s2[4].equals("0"))
//							{
//								danwei = "元";
//							}
//							else if(s2[4].equals("1"))
//							{
//								danwei = "元/人";
//							}
//							else if(s2[4].equals("2"))
//							{
//								danwei = "效益％";
//							}
//							rtS += "当值处于"+s2[0]+"-"+s2[1]+"之间,"+eq+s2[3]+"分;";
//						}
//						System.out.println( rtS);
//					}
//					else if(vo.detail_type == 2)
//					{
//						if(vo.detail_vo == null)
//						{
//							
//						}
//						else
//						{
//							System.out.println( vo.detail_vo.display_name);
//						}
//					}
//				}
			}
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		
		return list;
	}

	public List<KpiDictItemVsOrganizationVo> getKpiByGuikou(String guikou_id) {
		try{
			Connection conn = getConnect();
			List<KpiDictItemVsOrganizationVo> rtList = new ArrayList();
			String replaceGuikouId = getSqlGuikouId(guikou_id);
			
			String sql =  "select "+DICT_KPI_ITEM_COLUMNS+
//				" (select name from t_sys_stat_department where t_sys_stat_department.id = k.guikou_id) as guikou_name " +
				" from DICT_KPI_ITEM" +
				" ," +
				" (" +
				"	select distinct kpi_id from KPI_VS_ORGANIZATION where guikou_id in (" +replaceGuikouId+")" +
				"	union all" +
				"	select distinct parent_id from KPI_DETAIL_VS_ORGANIZATION where guikou_id in (" +replaceGuikouId+")"+
				" ) as k" +
				" where k.kpi_id=DICT_KPI_ITEM.id  " +
				" order by standard_type,detail_type,display_name";
//			String sql =  "select "+DICT_KPI_ITEM_COLUMNS+","+guikou_id+"," +
////					" (select name from t_sys_stat_department where t_sys_stat_department.id = KPI_VS_ORGANIZATION.kaohe_id) as kaohe_name," +
//					" (select name from t_sys_stat_department where t_sys_stat_department.id = "+guikou_id+") as guikou_name " +
//					" from DICT_KPI_ITEM" +
//					" ," +
//					" (select distinct guikou_id,kpi_id" +
//					" from KPI_VS_ORGANIZATION" +
//					" where guikou_id = " +guikou_id+
//					" ) as k" +
//					" where k.kpi_id=DICT_KPI_ITEM.id  " +
//					" order by standard_type,detail_type,display_name";
//			String sql =  "select "+DICT_KPI_ITEM_COLUMNS+","+guikou_id+"," +
////			" (select name from t_sys_stat_department where t_sys_stat_department.id = KPI_VS_ORGANIZATION.kaohe_id) as kaohe_name," +
//			" (select name from t_sys_stat_department where t_sys_stat_department.id = "+guikou_id+") as guikou_name " +
//			" from DICT_KPI_ITEM" +
//			" left join" +
//			" (select distinct guikou_id,kpi_id" +
//			" from KPI_VS_ORGANIZATION" +
//			" where guikou_id = " +guikou_id+
//			" ) as k" +
//			" on k.kpi_id=DICT_KPI_ITEM.id  " +
//			" order by id";
	
			ResultSet rs = queryData(conn,sql);
			
			KpiDictItemVsOrganizationVo vo ;
			while(rs.next()){
				vo = new KpiDictItemVsOrganizationVo();
				vo.id = rs.getInt(1);
				vo.item_name = rs.getString(2);
				vo.display_name = rs.getString(3);
				vo.description = rs.getString(4);
				vo.frequence = rs.getString(5);
				vo.point = DataFormater.numberToString(rs.getBigDecimal(6));
				vo.target_value = DataFormater.numberToString(rs.getBigDecimal(7));
				vo.target_type = rs.getInt(8);
				vo.standard_type = rs.getInt(9);
				vo.detail_type = rs.getInt(10);
				vo.different_scale = DataFormater.numberToString(rs.getBigDecimal(11));
				vo.add_type = rs.getInt(12);
				vo.scale_point = DataFormater.numberToString(rs.getBigDecimal(13));
				vo.formula = rs.getString(14);
				vo.default_value = DataFormater.numberToString(rs.getBigDecimal(15));
				vo.section_detail = rs.getString(16);
				vo.danwei = rs.getInt(17);
//				vo.kaohe_id = rs.getInt(18);
//				vo.guikou_id = rs.getInt(18);
//				vo.kaohe_name = rs.getString(20);
//				vo.guikou_name = rs.getString(19);
				rtList.add(vo);
			}
			freeConnection(conn);
			return rtList;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}

	public List<KpiDictItemVsOrganizationVo> getKpiByGuikou(String guikou_id,Connection conn) {
		try{
			List<KpiDictItemVsOrganizationVo> rtList = new ArrayList();
			String replaceGuikouId = getSqlGuikouId(guikou_id);
			
			String sql =  "select "+DICT_KPI_ITEM_COLUMNS+" from DICT_KPI_ITEM" +
				" ," +
				" (" +
				"	select distinct kpi_id from KPI_VS_ORGANIZATION where guikou_id in (" +replaceGuikouId+")" +
				"	union all" +
				"	select distinct parent_id from KPI_DETAIL_VS_ORGANIZATION where guikou_id in (" +replaceGuikouId+")"+
				" ) as k" +
				" where k.kpi_id=DICT_KPI_ITEM.id  " +
				" order by standard_type,detail_type,display_name";
	
			ResultSet rs = queryData(conn,sql);
			
			KpiDictItemVsOrganizationVo vo ;
			while(rs.next()){
				vo = new KpiDictItemVsOrganizationVo();
				vo.id = rs.getInt(1);
				vo.item_name = rs.getString(2);
				vo.display_name = rs.getString(3);
				vo.description = rs.getString(4);
				vo.frequence = rs.getString(5);
				vo.point = DataFormater.numberToString(rs.getBigDecimal(6));
				vo.target_value = DataFormater.numberToString(rs.getBigDecimal(7));
				vo.target_type = rs.getInt(8);
				vo.standard_type = rs.getInt(9);
				vo.detail_type = rs.getInt(10);
				vo.different_scale = DataFormater.numberToString(rs.getBigDecimal(11));
				vo.add_type = rs.getInt(12);
				vo.scale_point = DataFormater.numberToString(rs.getBigDecimal(13));
				vo.formula = rs.getString(14);
				vo.default_value = DataFormater.numberToString(rs.getBigDecimal(15));
				vo.section_detail = rs.getString(16);
				vo.danwei = rs.getInt(17);
//				vo.kaohe_id = rs.getInt(18);
//				vo.guikou_id = rs.getInt(18);
//				vo.kaohe_name = rs.getString(20);
//				vo.guikou_name = rs.getString(19);
				rtList.add(vo);
			}
			return rtList;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}

	public List<KpiDictItemVsOrganizationVo> getKpiByKaohe(int kaohe_id,String item_name) {
		try{
			Connection conn = getConnect();
			String sql = "select " +
					"	(select (select DICT_DUTY_TYPE.name from DICT_DUTY_TYPE where DICT_DUTY_TYPE.id = duty_id)+'-'+name from t_sys_stat_department where t_sys_stat_department.id = "+kaohe_id+") as kaohe_name," +
					"	p.id as pid,p.item_name as item_name,p.display_name as display_name,p.frequence as frequence," +
					"	p.target_type as target_type,p.standard_type as standard_type,p.detail_type as detail_type," +
					//point,target_value,different_scale,add_type,scale_point,formula,default_value,section_detail,danwei
					"	p.description as op_description,p.point as op_point,p.target_value as op_target_value,p.different_scale as op_different_scale," +
					"	p.add_type as op_add_type,p.scale_point as op_scale_point,p.formula as op_formula," +
					"	p.default_value as op_default_value,p.section_detail as op_section_detail,p.danwei as op_danwei," +
					"	(select " +
					"		(select " +
					"			(select DICT_DUTY_TYPE.name from DICT_DUTY_TYPE where DICT_DUTY_TYPE.id = duty_id)+'-'+name from t_sys_stat_department where t_sys_stat_department.id = guikou_id" +
					"		)" +
					" 	from KPI_VS_ORGANIZATION where p.id = KPI_VS_ORGANIZATION.kpi_id and KPI_VS_ORGANIZATION.kaohe_id = "+kaohe_id+") as guikou_name," +
					"	(select top 1 guikou_id from KPI_VS_ORGANIZATION where p.id = KPI_VS_ORGANIZATION.kpi_id and KPI_VS_ORGANIZATION.kaohe_id = "+kaohe_id+") as guikou_id," +
//					p.id as pid,p.item_name,p.display_name,p.description,p.frequence,p.target_type,p.standard_type,p.detail_type," +
					//point,target_value,different_scale,add_type,scale_point,formula,default_value,section_detail,danwei
					"	(select description from KPI_VS_ORGANIZATION where p.id = KPI_VS_ORGANIZATION.kpi_id and KPI_VS_ORGANIZATION.kaohe_id = "+kaohe_id+") as description," +
					"	(select point from KPI_VS_ORGANIZATION where p.id = KPI_VS_ORGANIZATION.kpi_id and KPI_VS_ORGANIZATION.kaohe_id = "+kaohe_id+") as point," +
					"	(select target_value from KPI_VS_ORGANIZATION where p.id = KPI_VS_ORGANIZATION.kpi_id and KPI_VS_ORGANIZATION.kaohe_id = "+kaohe_id+") as target_value," +
					"	(select different_scale from KPI_VS_ORGANIZATION where p.id = KPI_VS_ORGANIZATION.kpi_id and KPI_VS_ORGANIZATION.kaohe_id = "+kaohe_id+") as different_scale," +
					"	(select add_type from KPI_VS_ORGANIZATION where p.id = KPI_VS_ORGANIZATION.kpi_id and KPI_VS_ORGANIZATION.kaohe_id = "+kaohe_id+") as add_type," +
					"	(select scale_point from KPI_VS_ORGANIZATION where p.id = KPI_VS_ORGANIZATION.kpi_id and KPI_VS_ORGANIZATION.kaohe_id = "+kaohe_id+") as scale_point," +
					"	(select formula from KPI_VS_ORGANIZATION where p.id = KPI_VS_ORGANIZATION.kpi_id and KPI_VS_ORGANIZATION.kaohe_id = "+kaohe_id+") as formula," +
					"	(select default_value from KPI_VS_ORGANIZATION where p.id = KPI_VS_ORGANIZATION.kpi_id and KPI_VS_ORGANIZATION.kaohe_id = "+kaohe_id+") as default_value," +
					"	(select section_detail from KPI_VS_ORGANIZATION where p.id = KPI_VS_ORGANIZATION.kpi_id and KPI_VS_ORGANIZATION.kaohe_id = "+kaohe_id+") as section_detail," +
					"	(select danwei from KPI_VS_ORGANIZATION where p.id = KPI_VS_ORGANIZATION.kpi_id and KPI_VS_ORGANIZATION.kaohe_id = "+kaohe_id+") as danwei," +
						
					"	c.id as cid,c.item_name as oc_item_name,c.display_name as oc_display_name,c.target_type as c_target_type," +
						//display_name,target_value,default_value,different_scale,add_type,scale_point,formula,no_double,danwei
					"	c.target_value as oc_target_value,c.default_value as oc_default_value,c.different_scale as oc_different_scale," +
					"	c.add_type as oc_add_type,c.scale_point as oc_scale_point,c.formula as oc_formula,c.no_double as oc_no_double,c.danwei as oc_danwei,"+
					"	(select guikou_id from KPI_DETAIL_VS_ORGANIZATION where c.id = KPI_DETAIL_VS_ORGANIZATION.kpi_detail_id and KPI_DETAIL_VS_ORGANIZATION.kaohe_id = "+kaohe_id+") as c_guikou_id," +
					"	(select " +
					"		(select " +
					"			(select DICT_DUTY_TYPE.name from DICT_DUTY_TYPE where DICT_DUTY_TYPE.id = duty_id)+'-'+name from t_sys_stat_department where t_sys_stat_department.id = guikou_id" +
					"		)" +
					" 	from KPI_DETAIL_VS_ORGANIZATION where c.id = KPI_DETAIL_VS_ORGANIZATION.kpi_detail_id and KPI_DETAIL_VS_ORGANIZATION.kaohe_id = "+kaohe_id+") as c_guikou_name," +
					"	(select item_name from KPI_DETAIL_VS_ORGANIZATION where c.id = KPI_DETAIL_VS_ORGANIZATION.kpi_detail_id and KPI_DETAIL_VS_ORGANIZATION.kaohe_id = "+kaohe_id+") as c_item_name," +
					"	(select display_name from KPI_DETAIL_VS_ORGANIZATION where c.id = KPI_DETAIL_VS_ORGANIZATION.kpi_detail_id and KPI_DETAIL_VS_ORGANIZATION.kaohe_id = "+kaohe_id+") as c_display_name," +
					"	(select target_value from KPI_DETAIL_VS_ORGANIZATION where c.id = KPI_DETAIL_VS_ORGANIZATION.kpi_detail_id and KPI_DETAIL_VS_ORGANIZATION.kaohe_id = "+kaohe_id+") as c_target_value," +
					"	(select different_scale from KPI_DETAIL_VS_ORGANIZATION where c.id = KPI_DETAIL_VS_ORGANIZATION.kpi_detail_id and KPI_DETAIL_VS_ORGANIZATION.kaohe_id = "+kaohe_id+") as c_different_scale," +
					"	(select add_type from KPI_DETAIL_VS_ORGANIZATION where c.id = KPI_DETAIL_VS_ORGANIZATION.kpi_detail_id and KPI_DETAIL_VS_ORGANIZATION.kaohe_id = "+kaohe_id+") as c_add_type," +
					"	(select scale_point from KPI_DETAIL_VS_ORGANIZATION where c.id = KPI_DETAIL_VS_ORGANIZATION.kpi_detail_id and KPI_DETAIL_VS_ORGANIZATION.kaohe_id = "+kaohe_id+") as c_scale_point," +
					"	(select formula from KPI_DETAIL_VS_ORGANIZATION where c.id = KPI_DETAIL_VS_ORGANIZATION.kpi_detail_id and KPI_DETAIL_VS_ORGANIZATION.kaohe_id = "+kaohe_id+") as c_formula," +
					"	(select no_double from KPI_DETAIL_VS_ORGANIZATION where c.id = KPI_DETAIL_VS_ORGANIZATION.kpi_detail_id and KPI_DETAIL_VS_ORGANIZATION.kaohe_id = "+kaohe_id+") as c_no_double," +
					"	(select danwei from KPI_DETAIL_VS_ORGANIZATION where c.id = KPI_DETAIL_VS_ORGANIZATION.kpi_detail_id and KPI_DETAIL_VS_ORGANIZATION.kaohe_id = "+kaohe_id+") as c_danwei" +
					"	from DICT_KPI_ITEM as p " +
					"	left join" +
					"	DICT_KPI_ITEM_DETAIL as c" +
					"	on p.id = c.parent_id" +
					(item_name.equals("")?"":"  where p.item_name like '%"+item_name+"%'")+
					"	order by p.id,c.id";// p.standard_type,p.item_name,oc_item_name";
			
			
//			String sql = "select" +
//					" (select name from t_sys_stat_department where id = x.guikou) as guikou_name," +
//					" "+kaohe_id+" as kaohe," +
//					" (select name from t_sys_stat_department where id = "+kaohe_id+") as kaohe_name," +
//					" *" +
//					" from" +
//					" (" +
//					" select" +
//					" case when detail_type = 2" +
//					" 	then (select guikou_id from KPI_VS_ORGANIZATION where kaohe_id = "+kaohe_id+" and kpi_id = p.id and kpi_detail_id = c.id)	" +
//							"else (select guikou_id from KPI_VS_ORGANIZATION where kaohe_id = "+kaohe_id+" and kpi_id = p.id)" +
//					" 	end" +
//					" 	as guikou," +
//					" p.id as pid,p.item_name as p_item_name," +
//					" 		p.description,p.point,p.standard_type,p.detail_type,p.frequence," +
//					" c.id as cid,c.item_name as c_item_name" +
//					"  from DICT_KPI_ITEM as p" +
//					" left join DICT_KPI_ITEM_DETAIL as c" +
//					" on p.id = c.parent_id" +
//					" ) as x " +
//					" order by standard_type,p_item_name,c_item_name";
			ResultSet rs = queryData(conn,sql);
			List<KpiDictItemVsOrganizationVo> rtList = new ArrayList();
			KpiDictItemVsOrganizationVo vo ;
			while(rs.next()){
				vo = new KpiDictItemVsOrganizationVo();
				vo.id = rs.getInt("pid");				
				vo.frequence = rs.getString("frequence");
				vo.point = DataFormater.numberToString(rs.getBigDecimal("point"));
				vo.standard_type = rs.getInt("standard_type");
				vo.detail_type = rs.getInt("detail_type");				
				vo.item_name = rs.getString("item_name");
				vo.description = rs.getString("description");
				vo.display_name = rs.getString("display_name");
				vo.target_type = rs.getInt("target_type");
				vo.target_value = DataFormater.numberToString(rs.getBigDecimal("target_value"));
				vo.different_scale =  DataFormater.numberToString(rs.getBigDecimal("different_scale"));
				vo.scale_point = DataFormater.numberToString(rs.getBigDecimal("scale_point"));
				vo.section_detail = rs.getString("section_detail");
				vo.add_type = rs.getInt("add_type");
				
				if(vo.detail_type == 2)//明细
				{
					vo.p_guikou_id = rs.getInt("guikou_id");
					vo.guikou_id = rs.getInt("c_guikou_id");
					vo.guikou_name = rs.getString("c_guikou_name");	
				}
				else
				{
					vo.p_guikou_id = 0;
					vo.guikou_id = rs.getInt("guikou_id");
					vo.guikou_name = rs.getString("guikou_name");
				}
				
				
				vo.kaohe_name = rs.getString("kaohe_name");
				vo.kaohe_id = kaohe_id;
				
				vo.detail_id = rs.getInt("cid");
				vo.c_item_name = rs.getString("c_item_name");
				vo.c_target_type = rs.getInt("c_target_type");
				
				//description,point,target_value,different_scale,add_type,scale_point,formula,default_value,section_detail,danwei
				vo.op_description = rs.getString("op_description");
				vo.op_point = DataFormater.numberToString(rs.getBigDecimal("op_point"));
				vo.op_target_value = DataFormater.numberToString(rs.getBigDecimal("op_target_value"));
				vo.op_different_scale = DataFormater.numberToString(rs.getBigDecimal("op_different_scale"));
				vo.op_scale_point = DataFormater.numberToString(rs.getBigDecimal("op_scale_point"));
				vo.op_formula = rs.getString("op_formula");
				vo.op_default_value = DataFormater.numberToString(rs.getBigDecimal("op_default_value"));
				vo.op_section_detail = rs.getString("op_section_detail");
				vo.op_danwei = rs.getInt("op_danwei");
				vo.op_add_type = rs.getInt("op_add_type");
				
				//display_name,target_value,default_value,different_scale,add_type,scale_point,formula,no_double,danwei
				vo.oc_item_name = rs.getString("oc_item_name");
				vo.oc_display_name = rs.getString("oc_display_name");
				vo.oc_target_value = DataFormater.numberToString(rs.getBigDecimal("oc_target_value"));
				vo.oc_default_value = DataFormater.numberToString(rs.getBigDecimal("oc_default_value"));
				vo.oc_different_scale = DataFormater.numberToString(rs.getBigDecimal("oc_different_scale"));
				vo.oc_add_type = rs.getInt("oc_add_type");
				vo.oc_scale_point = DataFormater.numberToString(rs.getBigDecimal("oc_scale_point"));
				vo.oc_formula = rs.getString("oc_formula");
				vo.oc_no_double = rs.getInt("oc_no_double");
				vo.oc_danwei = rs.getInt("oc_danwei");
				
				vo.c_display_name = rs.getString("c_display_name");
				vo.c_target_value = rs.getString("c_target_value");
				vo.c_different_scale = rs.getString("c_different_scale");
				vo.c_add_type = rs.getInt("c_add_type");
				vo.c_scale_point = DataFormater.numberToString(rs.getBigDecimal("c_scale_point"));
				vo.c_formula = rs.getString("c_formula");
				vo.c_no_double = rs.getInt("c_no_double");
				vo.c_danwei = rs.getInt("c_danwei");
				
				rtList.add(vo);
			}
			
			int pid = 0;//增加父节点。
			for(int i = 0;i<rtList.size();i++)
			{
				KpiDictItemVsOrganizationVo tvo = rtList.get(i);
				if(tvo.detail_type == 2 && tvo.id != pid)
				{
					KpiDictItemVsOrganizationVo tvo2 = new KpiDictItemVsOrganizationVo();
					tvo2.id = tvo.id;
					tvo2.detail_id = 0;
					tvo2.item_name = tvo.item_name;
					tvo2.display_name = tvo.display_name;
					tvo2.op_description = tvo.op_description;
					tvo2.description = tvo.description;
					tvo2.c_item_name = tvo.c_item_name;
					tvo2.frequence = tvo.frequence;
					tvo2.point = tvo.point ;
					tvo2.standard_type = tvo.standard_type;
					tvo2.detail_type = tvo.detail_type;
					tvo2.guikou_id = tvo.p_guikou_id ;
					tvo2.guikou_name = "";
					tvo2.kaohe_name = tvo.kaohe_name;
					tvo2.kaohe_id= tvo.kaohe_id;
					rtList.add(i, tvo2);
					i++;
					pid = tvo.id;
				}
			}
			freeConnection(conn);
			return rtList;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}

	public void addKpiVsKaohe(int kpi_id, int kaohe_id, int guikou_id,int detail_id) {
		try{
			Connection conn = getConnect();
			String sql= "";
			if(detail_id == -1)//全体父子都赋值
			{
				//先删除全体对应d
				sql = "delete KPI_VS_ORGANIZATION where kpi_id = "+kpi_id +" and kaohe_id = "+kaohe_id ;
				updateData(conn, sql);
				sql = "delete KPI_DETAIL_VS_ORGANIZATION where parent_id = "+kpi_id +" and kaohe_id = "+kaohe_id ;
				updateData(conn, sql);
				
				sql ="insert into KPI_VS_ORGANIZATION(add_type,danwei,default_value,description," +
					"	detail_type,different_scale,display_name,formula,frequence,guikou_id," +
					"	item_name,kaohe_id,kpi_id,point,scale_point,section_detail," +
					"	standard_type,target_type,target_value) " +
					"	select add_type,danwei,default_value,description," +
					"	detail_type,different_scale,display_name,formula,frequence,-1," +
					"	item_name,"+kaohe_id+",id,point,scale_point,section_detail," +
					"	standard_type,target_type,target_value" +
					"	from DICT_KPI_ITEM where id = "+kpi_id;
				updateData(conn, sql);
				sql = "insert into KPI_DETAIL_VS_ORGANIZATION(add_type,danwei,default_value,different_scale," +
					"	display_name,formula,guikou_id,item_name,kaohe_id,kpi_detail_id,no_double," +
					"	parent_id,scale_point,target_type,target_value)" +
					"	select add_type,danwei,default_value,different_scale," +
					"	display_name,formula,"+guikou_id+",item_name,"+kaohe_id+",id,no_double," +
					"	parent_id,scale_point,target_type,target_value" +
					"	from DICT_KPI_ITEM_DETAIL where parent_id = "+kpi_id;
				updateData(conn, sql);
			}
			else if(detail_id != 0)//具体子
			{
				sql = "select count(1) from KPI_VS_ORGANIZATION where kpi_id = "+kpi_id +" and kaohe_id = "+kaohe_id ;
				ResultSet rs = queryData(conn, sql);
				if(rs.next())
				{
					if(rs.getInt(1) == 0)
					{
						sql ="insert into KPI_VS_ORGANIZATION(add_type,danwei,default_value,description," +
							"	detail_type,different_scale,display_name,formula,frequence,guikou_id," +
							"	item_name,kaohe_id,kpi_id,point,scale_point,section_detail," +
							"	standard_type,target_type,target_value) " +
							"	select add_type,danwei,default_value,description," +
							"	detail_type,different_scale,display_name,formula,frequence,-1," +
							"	item_name,"+kaohe_id+",id,point,scale_point,section_detail," +
							"	standard_type,target_type,target_value" +
							"	from DICT_KPI_ITEM where id = "+kpi_id;
						updateData(conn, sql);
					}
				}
				
				sql = "insert into KPI_DETAIL_VS_ORGANIZATION(add_type,danwei,default_value,different_scale," +
					"	display_name,formula,guikou_id,item_name,kaohe_id,kpi_detail_id,no_double," +
					"	parent_id,scale_point,target_type,target_value)" +
					"	select add_type,danwei,default_value,different_scale," +
					"	display_name,formula,"+guikou_id+",item_name,"+kaohe_id+","+detail_id+",no_double," +
					"	parent_id,scale_point,target_type,target_value" +
					"	from DICT_KPI_ITEM_DETAIL where id = "+detail_id;
				updateData(conn, sql);
			}
			else//基本项目赋值
			{
				sql ="insert into KPI_VS_ORGANIZATION(add_type,danwei,default_value,description," +
					"detail_type,different_scale,display_name,formula,frequence,guikou_id," +
					"item_name,kaohe_id,kpi_id,point,scale_point,section_detail," +
					"standard_type,target_type,target_value) " +
					" select add_type,danwei,default_value,description," +
					"detail_type,different_scale,display_name,formula,frequence,"+guikou_id+"," +
					"item_name,"+kaohe_id+",id,point,scale_point,section_detail," +
					"standard_type,target_type,target_value" +
					" from DICT_KPI_ITEM where id = "+kpi_id;
				updateData(conn, sql);
			}
//			System.out.println(sql);
//			conn.setAutoCommit(false);
//			PreparedStatement pst=conn.prepareStatement(sql);
//			pst.setInt(1, kpi_id);
//			pst.setInt(2, kaohe_id);
//			pst.setInt(3, guikou_id);
//			pst.setInt(4, detail_id);
//			
//			pst.executeUpdate();
//			pst.close();
//			conn.commit();			
			
			freeConnection(conn);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}

	public void deleteKpiVsKaohe(int kpi_id, int kaohe_id,int detail_id) {
		try{
			Connection conn = getConnect();
			
			String sql= "";
			if(detail_id == 0)//全体父子都删除
			{
				sql = "delete KPI_VS_ORGANIZATION where kpi_id = "+kpi_id +" and kaohe_id = "+kaohe_id ;
				updateData(conn, sql);
				sql = "delete KPI_DETAIL_VS_ORGANIZATION where parent_id = "+kpi_id +" and kaohe_id = "+kaohe_id ;
				updateData(conn, sql);
			}
			else if(detail_id != 0)//具体子
			{
				sql = "delete KPI_DETAIL_VS_ORGANIZATION where parent_id = "+kpi_id +" and kaohe_id = "+kaohe_id+" and kpi_detail_id = "+detail_id ;
				updateData(conn, sql);
				
				sql = "select count(1) from KPI_DETAIL_VS_ORGANIZATION where parent_id = "+kpi_id +" and kaohe_id = "+kaohe_id;
				ResultSet rs = queryData(conn, sql);
				if(rs.next())
				{
					if(rs.getInt(1) == 0)
					{
						sql = "delete KPI_VS_ORGANIZATION where kpi_id = "+kpi_id +" and kaohe_id = "+kaohe_id ;
						updateData(conn, sql);
					}
				}
			}
//			else//基本项目
//			{
//				sql ="delete KPI_VS_ORGANIZATION where kpi_id="+kpi_id+
//					" and kaohe_id = "+kaohe_id;
//				updateData(conn, sql);
//			}
			
			
			
			freeConnection(conn);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}

	public List<KpiDictItemVo> getKpiItemByGuikou(int guikou_id) {
		try{
			Connection conn = getConnect();
			String sql = "";
			if(guikou_id == 0){
				sql = "select "+DICT_KPI_ITEM_COLUMNS+" from DICT_KPI_ITEM order by id";
			}else{
				sql = "select "+DICT_KPI_ITEM_COLUMNS+" from DICT_KPI_ITEM where id in" +
				" (" +
				" select kpi_id from KPI_VS_ORGANIZATION where guikou_id = " +guikou_id+
				" ) order by id";
			}

			ResultSet rs = queryData(conn,sql);
			List<KpiDictItemVo> rtList = new ArrayList();
			KpiDictItemVo vo ;
			while(rs.next()){
//				"id,item_name,display_name,description,frequence,"+//id，名称，显示名称，描述，频次（0001，0002，0003，0004）
//				"point,target_value,"+//分数，标准值
//				"target_type,"+//1越高越好、2越低越好
//				"standard_type,"+//0标准、1奖惩、2一票
//				"detail_type,"+//0标准、1区间、2明细、3录入
//				"different_scale,"+//每偏离？
//				"add_type,"+//偏离的话，是增加还是减少  1增2减0等
//				"scale_point,"+//偏离一个单位，扣/加的分数
//				"formula,"+//整理后的公式
//				"default_value," +//默认值
//				"section_detail,danwei
				vo = new KpiDictItemVo();
				vo.id = rs.getInt(1);
				vo.item_name = rs.getString(2);
				vo.display_name = rs.getString(3);
				vo.description = rs.getString(4);
				vo.frequence = rs.getString(5);
				vo.point = DataFormater.numberToString(rs.getBigDecimal(6));
				vo.target_value = DataFormater.numberToString(rs.getBigDecimal(7));
				vo.target_type = rs.getInt(8);
				vo.standard_type = rs.getInt(9);
				vo.detail_type = rs.getInt(10);
				vo.different_scale = DataFormater.numberToString(rs.getBigDecimal(11));
				vo.add_type = rs.getInt(12);
				vo.scale_point = DataFormater.numberToString(rs.getBigDecimal(13));
				vo.formula = rs.getString(14);
				vo.default_value = DataFormater.numberToString(rs.getBigDecimal(15));
				vo.section_detail = rs.getString(16);
				vo.danwei = rs.getInt(17);
				rtList.add(vo);
			}
			freeConnection(conn);
			return rtList;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	private String getSqlGuikouId(String guikou_id)
	{
		String replaceGuikouId = "";
		if(guikou_id == null || guikou_id.trim().equals(""))
		{
			return null;
		}
		else if(guikou_id.equals("0"))
		{
			return "0";
		}
		else
		{
			replaceGuikouId = guikou_id.replaceAll(":", ",");
			if(",".equals(replaceGuikouId.charAt(replaceGuikouId.length()-1)+""))
			{
				replaceGuikouId = replaceGuikouId.substring(0,replaceGuikouId.length()-1);
			}			
		}
		return replaceGuikouId;
	}
	public List<KpiValueVo> getKpiPointByGuikouItem(String guikou_ids,int kpi_id,int y,int m) {
		try{
			int isOverDue = isOverDue(y,m);
			Connection conn = getConnect();
			String replaceGuikou = getSqlGuikouId(guikou_ids);//由于用户可能同事具有多个科室的权限，因此guikou_ids也是多选的。
			String sql = "select m.kpi_id,m.item_name,m.display_name,m.point,m.target_value," +
					"	m.description,m.frequence,m.target_type,m.standard_type,m.detail_type," +
					"	m.different_scale,m.add_type,m.scale_point,m.formula,m.default_value," +
					"	m.section_detail,m.danwei," +
					"	m.kaohe_id,(select name from dbo.t_sys_stat_department where t_sys_stat_department.id = m.kaohe_id) as kaohe_name," +
					"	m.guikou_id,(select name from dbo.t_sys_stat_department where t_sys_stat_department.id = m.guikou_id) as guikou_name," +
					"	m.c_id,m.c_item_name,m.c_display_name,m.c_target_value,m.c_default_value," +
					"	m.c_target_type,m.c_different_scale,m.c_add_type,m.c_scale_point," +
					"	m.c_formula,m.c_no_double,m.c_danwei,m.kpi_detail_id," +
					"	kv.id as v_id,kdv.id as vc_id," +
					"   kv.value," +y+" as year,"+m+" as month,"+
					"	kv.create_user,kv.create_time,kv.score,kv.update_score,kv.detail,kv.valid_detail," +
					"	kv.update_user,kv.update_time,kv.update_description,kv.valid,kv.valid_user,kv.valid_time," +
					"	kdv.value as c_value,kdv.score as c_score," +
					"	kdv.create_user as c_create_user,kdv.create_time as c_create_time,kdv.detail as c_detail," +
					"	kdv.update_score as c_update_score,kdv.update_user as c_update_user,kdv.update_time as c_update_time," +
					"	kdv.valid as c_valid,kdv.valid_user as c_valid_user,kdv.valid_time as c_valid_time" +
					
					"   from" +
					"   (" +
					"	select "+y+" as year,"+m+" as month,kpi_id,item_name,display_name,point,target_value,description,frequence,target_type," +
					"	standard_type,detail_type,different_scale,add_type,scale_point,formula,default_value," +
					"	section_detail,danwei,kaohe_id," +
					"	null as c_id,null as c_item_name,null as c_display_name,null as c_target_value,null as c_default_value,null as c_target_type," +
					"	null as c_different_scale,null as c_add_type,null as c_scale_point,null as c_formula,null as c_no_double,null as c_danwei," +
					"	1 as kpi_detail_id," +
					"	guikou_id from KPI_VS_ORGANIZATION " +
					"	where kpi_id = " +kpi_id+" and guikou_id != -1 " +
					(replaceGuikou.equals("0")?"":"	and guikou_id in (" +replaceGuikou+")")+
					"	union all" +
					"	select "+y+" as year,"+m+" as month," +
					"	a.kpi_id,a.item_name,a.display_name,a.point,a.target_value,a.description,a.frequence,a.target_type," +
					"	a.standard_type,a.detail_type,a.different_scale,a.add_type,a.scale_point,a.formula,a.default_value," +
					"	a.section_detail,a.danwei,a.kaohe_id," +
					"	b.kpi_detail_id as c_id,b.item_name as c_item_name,b.display_name as c_display_name," +
					"	b.target_value as c_target_value,b.default_value as c_default_value,b.target_type as c_target_type," +
					"	b.different_scale as c_different_scale,b.add_type as c_add_type,b.scale_point as c_scale_point," +
					"	b.formula as c_formula,b.no_double as c_no_double,b.danwei as c_danwei," +
					"	1 as kpi_detail_id,b.guikou_id from (" +
					"	select KPI_VS_ORGANIZATION.*,KPI_DETAIL_VS_ORGANIZATION.kpi_detail_id from KPI_VS_ORGANIZATION,KPI_DETAIL_VS_ORGANIZATION" +
					"	where KPI_VS_ORGANIZATION.kaohe_id = KPI_DETAIL_VS_ORGANIZATION.kaohe_id" +
					"	and KPI_VS_ORGANIZATION.guikou_id = -1" +
					"	and KPI_VS_ORGANIZATION.kpi_id = KPI_DETAIL_VS_ORGANIZATION.parent_id" +
					(replaceGuikou.equals("0")?"":"	and KPI_DETAIL_VS_ORGANIZATION.guikou_id in (" +replaceGuikou+")")+
					"	) as a" +
					"	left join KPI_DETAIL_VS_ORGANIZATION as b" +
					"	on a.kpi_id = b.parent_id" +
					"	and a.kaohe_id = b.kaohe_id" +
					"	and a.kpi_detail_id = b.kpi_detail_id" +
					"	where a.kpi_id = " +kpi_id+					
					
//					"	select "+y+" as year,"+m+" as month,pk.id as kpi_id,pk.item_name,pk.display_name,pk.point,pk.target_value," +
//					"	pk.description,pk.frequence,pk.target_type,pk.standard_type,pk.detail_type," +
//					"	pk.different_scale,pk.add_type,pk.scale_point,pk.formula,pk.default_value," +
//					"	pk.section_detail,pk.danwei,pk.kaohe_id,pk.guikou_id," +
//					"	kd.id as c_id,kd.item_name as c_item_name,kd.display_name as c_display_name," +
//					"	kd.target_value as c_target_value,kd.default_value as c_default_value,kd.target_type as c_target_type," +
//					"	kd.different_scale as c_different_scale,kd.add_type as c_add_type,kd.scale_point as c_scale_point," +
//					"	kd.formula as c_formula,kd.no_double as c_no_double,kd.danwei as c_danwei," +
//					"	case when pk.detail_type = 2 then" +
//					"	(select count(1) from KPI_VS_ORGANIZATION as t" +
//					"	where t.kaohe_id = pk.kaohe_id and t.guikou_id = pk.guikou_id and pk.id = pk.id and t.kpi_detail_id = kd.id)" +
//					"	else 1 end as kpi_detail_id  " +
//					"	from" +
//					"	(" +
//					"	select * from DICT_KPI_ITEM as dk,	KPI_VS_ORGANIZATION as ko" +
//					"	where dk.id = ko.kpi_id" +
//					(replaceGuikou.equals("0")?"":"	and ko.guikou_id in (" +replaceGuikou+")")+
//					"	and ko.kpi_id = " +kpi_id+
//					"	) as pk" +
//					"	left join  DICT_KPI_ITEM_DETAIL as kd" +
//					"	on kd.id = pk.kpi_detail_id" +
					
					
					
					
					
					"   ) as m" +
					"   left join KPI_VALUE as kv" +
					"   on" +
					"   m.kpi_id = kv.kpi_id" +
					"   and kv.year = m.year and kv.month = m.month" +
//					"   and kv.guikou_id = m.guikou_id" +
					"	and kv.kaohe_id = m.kaohe_id " +
					"   left join KPI_DETAIL_VALUE as kdv" +
					"   on" +
					"   kdv.year = m.year and kdv.month = m.month" +
					"   and kdv.kpi_detail_id = m.c_id" +
					"   and kdv.guikou_id = m.guikou_id" +
					"   and kdv.kaohe_id = m.kaohe_id" +
					"   order by kaohe_name";
				
			ResultSet rs = queryData(conn,sql);
			List<KpiValueVo> rtList = new ArrayList();
			KpiValueVo vo ;
			while(rs.next()){
				vo = new KpiValueVo();
				vo.is_over_due = isOverDue;
				
				vo.is_my_detail = rs.getInt("kpi_detail_id");//一个父下面可以有几个明细，但是可能并非所有明细都是本父科室的。
				
				vo.kpi_id	=	rs.getInt("kpi_id");
				vo.kpi_name	=	rs.getString("item_name");
				vo.display_name	=	rs.getString("display_name");
				vo.point	=	DataFormater.numberToString(rs.getBigDecimal("point"));
				vo.target_value	=	DataFormater.numberToString(rs.getBigDecimal("target_value"));
				vo.description	=	rs.getString("description");
				vo.frequence	=	rs.getString("frequence");
				vo.target_type	=	rs.getInt("target_type");
				vo.standard_type	=	rs.getInt("standard_type");
				vo.detail_type	=	rs.getInt("detail_type");
				vo.different_scale	=	DataFormater.numberToString(rs.getBigDecimal("different_scale"));
				vo.add_type	=	rs.getInt("add_type");
				vo.scale_point	=	DataFormater.numberToString(rs.getBigDecimal("scale_point"));
				vo.formula	=	rs.getString("formula");
				vo.default_value	=	rs.getString("default_value");
				vo.section_detail	=	rs.getString("section_detail");
				vo.danwei	=	rs.getInt("danwei");
				vo.kaohe_id	=	rs.getInt("kaohe_id");
				vo.kaohe_name	=	rs.getString("kaohe_name");
				vo.guikou_id	=	rs.getInt("guikou_id");
				vo.guikou_name	=	rs.getString("guikou_name");
				vo.detail = rs.getString("detail");
				
				vo.value	=	DataFormater.numberToString(rs.getBigDecimal("value"));
				if(vo.value != null && !vo.value.equals(""))
				{
					vo.tmp_value = vo.value;
				}
				else
				{
					vo.tmp_value = vo.default_value;
				}
				
				vo.create_user	=	rs.getString("create_user");
				vo.create_time	=	rs.getDate("create_time");
				
				vo.score	=	rs.getString("score");				
				vo.update_score	=	DataFormater.numberToString(rs.getBigDecimal("update_score"));
				vo.tmp_update_score	=	vo.update_score;
				
				vo.update_user	=	rs.getString("update_user");
				vo.update_time	=	rs.getDate("update_time");
				vo.update_description	=	rs.getString("update_description");
				vo.valid	=	rs.getInt("valid");
				vo.tmp_valid	=	vo.valid;
				vo.valid_user	=	rs.getString("valid_user");
				vo.valid_time	=	rs.getDate("valid_time");
				vo.valid_detail  = rs.getString("valid_detail");
				
				vo.v_id = rs.getInt("v_id");
				vo.vc_id = rs.getInt("vc_id");
				vo.c_id	= rs.getInt("c_id");
				vo.c_item_name	=	rs.getString("c_item_name");
				vo.c_display_name	=	rs.getString("c_display_name");
				vo.c_target_value	=	DataFormater.numberToString(rs.getBigDecimal("c_target_value"));
				vo.c_default_value	=	DataFormater.numberToString(rs.getBigDecimal("c_default_value"));
				vo.c_target_type	=	rs.getInt("c_target_type");
				vo.c_different_scale	=	DataFormater.numberToString(rs.getBigDecimal("c_different_scale"));
				vo.c_add_type	=	rs.getInt("c_add_type");
				vo.c_scale_point	=	DataFormater.numberToString(rs.getBigDecimal("c_scale_point"));
				vo.c_formula	=	rs.getString("c_formula");
				vo.c_no_double	=	rs.getInt("c_no_double");
				vo.c_danwei	=	rs.getInt("c_danwei");
//				vo.id	=	rs.getInt("id");
				vo.year	=	rs.getInt("year");
				vo.month	=	rs.getInt("month");				
				vo.c_id	=	rs.getInt("c_id");
				vo.c_value	=	DataFormater.numberToString(rs.getBigDecimal("c_value"));
				if(vo.c_value != null && !vo.c_value.equals(""))
				{
					vo.c_tmp_value = vo.c_value;
				}
				else
				{
					vo.c_tmp_value = vo.c_target_value;
				}
				vo.c_score	=	DataFormater.numberToString(rs.getBigDecimal("c_score"));
				vo.c_create_user	=	rs.getString("c_create_user");
				vo.c_create_time	=	rs.getDate("c_create_time");
				vo.c_update_score	=	DataFormater.numberToString(rs.getBigDecimal("c_update_score"));
				vo.c_tmp_update_score	=	vo.c_update_score;
				vo.c_update_user	=	rs.getString("c_update_user");
				vo.c_update_time	=	rs.getDate("c_update_time");
				vo.c_valid	=	rs.getInt("c_valid");
				vo.c_valid_user	=	rs.getString("c_valid_user");
				vo.c_valid_time	=	rs.getDate("c_valid_time");
				vo.c_detail = rs.getString("c_detail");
//				System.out.println(">>>>>>>>>>>>>>>>>>>>>"+vo.score);
				rtList.add(vo);
			}
			freeConnection(conn);
			return rtList;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	public List<KpiValueVo> getKpiPointByGuikouItem(String guikou_ids,int kpi_id,int y,int m,Connection conn) {
		try{
			int isOverDue = isOverDue(y,m);
			String replaceGuikou = getSqlGuikouId(guikou_ids);//由于用户可能同事具有多个科室的权限，因此guikou_ids也是多选的。
			String sql = "select m.kpi_id,m.item_name,m.display_name,m.point,m.target_value," +
					"	m.description,m.frequence,m.target_type,m.standard_type,m.detail_type," +
					"	m.different_scale,m.add_type,m.scale_point,m.formula,m.default_value," +
					"	m.section_detail,m.danwei," +
					"	m.kaohe_id,(select name from dbo.t_sys_stat_department where t_sys_stat_department.id = m.kaohe_id) as kaohe_name," +
					"	m.guikou_id,(select name from dbo.t_sys_stat_department where t_sys_stat_department.id = m.guikou_id) as guikou_name," +
					"	m.c_id,m.c_item_name,m.c_display_name,m.c_target_value,m.c_default_value," +
					"	m.c_target_type,m.c_different_scale,m.c_add_type,m.c_scale_point," +
					"	m.c_formula,m.c_no_double,m.c_danwei,m.kpi_detail_id," +
					"	kv.id as v_id,kdv.id as vc_id," +
					"   kv.value," +y+" as year,"+m+" as month,"+
					"	kv.create_user,kv.create_time,kv.score,kv.update_score,kv.detail,kv.valid_detail," +
					"	kv.update_user,kv.update_time,kv.update_description,kv.valid,kv.valid_user,kv.valid_time," +
					"	kdv.value as c_value,kdv.score as c_score," +
					"	kdv.create_user as c_create_user,kdv.create_time as c_create_time,kdv.detail as c_detail," +
					"	kdv.update_score as c_update_score,kdv.update_user as c_update_user,kdv.update_time as c_update_time," +
					"	kdv.valid as c_valid,kdv.valid_user as c_valid_user,kdv.valid_time as c_valid_time" +
					
					"   from" +
					"   (" +
					"	select "+y+" as year,"+m+" as month,kpi_id,item_name,display_name,point,target_value,description,frequence,target_type," +
					"	standard_type,detail_type,different_scale,add_type,scale_point,formula,default_value," +
					"	section_detail,danwei,kaohe_id," +
					"	null as c_id,null as c_item_name,null as c_display_name,null as c_target_value,null as c_default_value,null as c_target_type," +
					"	null as c_different_scale,null as c_add_type,null as c_scale_point,null as c_formula,null as c_no_double,null as c_danwei," +
					"	1 as kpi_detail_id," +
					"	guikou_id from KPI_VS_ORGANIZATION " +
					"	where kpi_id = " +kpi_id+" and guikou_id != -1 " +
					(replaceGuikou.equals("0")?"":"	and guikou_id in (" +replaceGuikou+")")+
					"	union all" +
					"	select "+y+" as year,"+m+" as month," +
					"	a.kpi_id,a.item_name,a.display_name,a.point,a.target_value,a.description,a.frequence,a.target_type," +
					"	a.standard_type,a.detail_type,a.different_scale,a.add_type,a.scale_point,a.formula,a.default_value," +
					"	a.section_detail,a.danwei,a.kaohe_id," +
					"	b.kpi_detail_id as c_id,b.item_name as c_item_name,b.display_name as c_display_name," +
					"	b.target_value as c_target_value,b.default_value as c_default_value,b.target_type as c_target_type," +
					"	b.different_scale as c_different_scale,b.add_type as c_add_type,b.scale_point as c_scale_point," +
					"	b.formula as c_formula,b.no_double as c_no_double,b.danwei as c_danwei," +
					"	1 as kpi_detail_id,b.guikou_id from (" +
					"	select KPI_VS_ORGANIZATION.*,KPI_DETAIL_VS_ORGANIZATION.kpi_detail_id from KPI_VS_ORGANIZATION,KPI_DETAIL_VS_ORGANIZATION" +
					"	where KPI_VS_ORGANIZATION.kaohe_id = KPI_DETAIL_VS_ORGANIZATION.kaohe_id" +
					"	and KPI_VS_ORGANIZATION.guikou_id = -1" +
					"	and KPI_VS_ORGANIZATION.kpi_id = KPI_DETAIL_VS_ORGANIZATION.parent_id" +
					(replaceGuikou.equals("0")?"":"	and KPI_DETAIL_VS_ORGANIZATION.guikou_id in (" +replaceGuikou+")")+
					"	) as a" +
					"	left join KPI_DETAIL_VS_ORGANIZATION as b" +
					"	on a.kpi_id = b.parent_id" +
					"	and a.kaohe_id = b.kaohe_id" +
					"	and a.kpi_detail_id = b.kpi_detail_id" +
					"	where a.kpi_id = " +kpi_id+					
					
					
					"   ) as m" +
					"   left join KPI_VALUE as kv" +
					"   on" +
					"   m.kpi_id = kv.kpi_id" +
					"   and kv.year = m.year and kv.month = m.month" +
//					"   and kv.guikou_id = m.guikou_id" +
					"	and kv.kaohe_id = m.kaohe_id " +
					"   left join KPI_DETAIL_VALUE as kdv" +
					"   on" +
					"   kdv.year = m.year and kdv.month = m.month" +
					"   and kdv.kpi_detail_id = m.c_id" +
					"   and kdv.guikou_id = m.guikou_id" +
					"   and kdv.kaohe_id = m.kaohe_id" +
					"   order by kaohe_name";
				
			ResultSet rs = queryData(conn,sql);
			List<KpiValueVo> rtList = new ArrayList();
			KpiValueVo vo ;
			while(rs.next()){
				vo = new KpiValueVo();
				vo.is_over_due = isOverDue;
				
				vo.is_my_detail = rs.getInt("kpi_detail_id");//一个父下面可以有几个明细，但是可能并非所有明细都是本父科室的。
				
				vo.kpi_id	=	rs.getInt("kpi_id");
				vo.kpi_name	=	rs.getString("item_name");
				vo.display_name	=	rs.getString("display_name");
				vo.point	=	DataFormater.numberToString(rs.getBigDecimal("point"));
				vo.target_value	=	DataFormater.numberToString(rs.getBigDecimal("target_value"));
				vo.description	=	rs.getString("description");
				vo.frequence	=	rs.getString("frequence");
				vo.target_type	=	rs.getInt("target_type");
				vo.standard_type	=	rs.getInt("standard_type");
				vo.detail_type	=	rs.getInt("detail_type");
				vo.different_scale	=	DataFormater.numberToString(rs.getBigDecimal("different_scale"));
				vo.add_type	=	rs.getInt("add_type");
				vo.scale_point	=	DataFormater.numberToString(rs.getBigDecimal("scale_point"));
				vo.formula	=	rs.getString("formula");
				vo.default_value	=	rs.getString("default_value");
				vo.section_detail	=	rs.getString("section_detail");
				vo.danwei	=	rs.getInt("danwei");
				vo.kaohe_id	=	rs.getInt("kaohe_id");
				vo.kaohe_name	=	rs.getString("kaohe_name");
				vo.guikou_id	=	rs.getInt("guikou_id");
				vo.guikou_name	=	rs.getString("guikou_name");
				vo.detail = rs.getString("detail");
				
				vo.value	=	DataFormater.numberToString(rs.getBigDecimal("value"));
				if(vo.value != null && !vo.value.equals(""))
				{
					vo.tmp_value = vo.value;
				}
				else
				{
					vo.tmp_value = vo.default_value;
				}
				
				vo.create_user	=	rs.getString("create_user");
				vo.create_time	=	rs.getDate("create_time");
				
				vo.score	=	rs.getString("score");				
				vo.update_score	=	DataFormater.numberToString(rs.getBigDecimal("update_score"));
				vo.tmp_update_score	=	vo.update_score;
				
				vo.update_user	=	rs.getString("update_user");
				vo.update_time	=	rs.getDate("update_time");
				vo.update_description	=	rs.getString("update_description");
				vo.valid	=	rs.getInt("valid");
				vo.tmp_valid	=	vo.valid;
				vo.valid_user	=	rs.getString("valid_user");
				vo.valid_time	=	rs.getDate("valid_time");
				vo.valid_detail  = rs.getString("valid_detail");
				
				vo.v_id = rs.getInt("v_id");
				vo.vc_id = rs.getInt("vc_id");
				vo.c_id	= rs.getInt("c_id");
				vo.c_item_name	=	rs.getString("c_item_name");
				vo.c_display_name	=	rs.getString("c_display_name");
				vo.c_target_value	=	DataFormater.numberToString(rs.getBigDecimal("c_target_value"));
				vo.c_default_value	=	DataFormater.numberToString(rs.getBigDecimal("c_default_value"));
				vo.c_target_type	=	rs.getInt("c_target_type");
				vo.c_different_scale	=	DataFormater.numberToString(rs.getBigDecimal("c_different_scale"));
				vo.c_add_type	=	rs.getInt("c_add_type");
				vo.c_scale_point	=	DataFormater.numberToString(rs.getBigDecimal("c_scale_point"));
				vo.c_formula	=	rs.getString("c_formula");
				vo.c_no_double	=	rs.getInt("c_no_double");
				vo.c_danwei	=	rs.getInt("c_danwei");
//				vo.id	=	rs.getInt("id");
				vo.year	=	rs.getInt("year");
				vo.month	=	rs.getInt("month");				
				vo.c_id	=	rs.getInt("c_id");
				vo.c_value	=	DataFormater.numberToString(rs.getBigDecimal("c_value"));
				if(vo.c_value != null && !vo.c_value.equals(""))
				{
					vo.c_tmp_value = vo.c_value;
				}
				else
				{
					vo.c_tmp_value = vo.c_target_value;
				}
				vo.c_score	=	DataFormater.numberToString(rs.getBigDecimal("c_score"));
				vo.c_create_user	=	rs.getString("c_create_user");
				vo.c_create_time	=	rs.getDate("c_create_time");
				vo.c_update_score	=	DataFormater.numberToString(rs.getBigDecimal("c_update_score"));
				vo.c_tmp_update_score	=	vo.c_update_score;
				vo.c_update_user	=	rs.getString("c_update_user");
				vo.c_update_time	=	rs.getDate("c_update_time");
				vo.c_valid	=	rs.getInt("c_valid");
				vo.c_valid_user	=	rs.getString("c_valid_user");
				vo.c_valid_time	=	rs.getDate("c_valid_time");
				vo.c_detail = rs.getString("c_detail");
				rtList.add(vo);
			}
			return rtList;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public List<KpiValueVo> getKpiPointByGuikouKaohe(String guikou_ids,int kaohe_id,int y,int m) {
		try{
			int isOverDue = isOverDue(y,m);
			
			Connection conn = getConnect();
			String replaceGuikou = getSqlGuikouId(guikou_ids);
			String sql = "select m.kpi_id,m.item_name,m.display_name,m.point,m.target_value," +
					"	m.description,m.frequence,m.target_type,m.standard_type,m.detail_type," +
					"	m.different_scale,m.add_type,m.scale_point,m.formula,m.default_value," +
					"	m.section_detail,m.danwei," +
					"	m.kaohe_id,(select name from dbo.t_sys_stat_department where t_sys_stat_department.id = m.kaohe_id) as kaohe_name," +
					"	m.guikou_id,(select name from dbo.t_sys_stat_department where t_sys_stat_department.id = m.guikou_id) as guikou_name," +
					"	m.c_id,m.c_item_name,m.c_display_name,m.c_target_value,m.c_default_value," +
					"	m.c_target_type,m.c_different_scale,m.c_add_type,m.c_scale_point," +
					"	m.c_formula,m.c_no_double,m.c_danwei,m.kpi_detail_id," +
					"	kv.id as v_id,kdv.id as vc_id," +
					"   kv.value," +y+" as year,"+m+" as month,"+
					"	kv.create_user,kv.create_time,kv.score,kv.update_score,kv.detail,kv.valid_detail," +
					"	kv.update_user,kv.update_time,kv.update_description,kv.valid,kv.valid_user,kv.valid_time," +
					"	kdv.value as c_value,kdv.score as c_score," +
					"	kdv.create_user as c_create_user,kdv.create_time as c_create_time,kdv.detail as c_detail," +
					"	kdv.update_score as c_update_score,kdv.update_user as c_update_user,kdv.update_time as c_update_time," +
					"	kdv.valid as c_valid,kdv.valid_user as c_valid_user,kdv.valid_time as c_valid_time" +
					
					"   from" +
					"   (" +
					"	select "+y+" as year,"+m+" as month,kpi_id,item_name,display_name,point,target_value,description,frequence,target_type," +
					"	standard_type,detail_type,different_scale,add_type,scale_point,formula,default_value," +
					"	section_detail,danwei,kaohe_id," +
					"	null as c_id,null as c_item_name,null as c_display_name,null as c_target_value,null as c_default_value,null as c_target_type," +
					"	null as c_different_scale,null as c_add_type,null as c_scale_point,null as c_formula,null as c_no_double,null as c_danwei," +
					"	1 as kpi_detail_id," +
					"	guikou_id from KPI_VS_ORGANIZATION " +
					"	where kaohe_id = " +kaohe_id+" and guikou_id != -1 " +
					(replaceGuikou.equals("0")?"":"	and guikou_id in (" +replaceGuikou+")")+
					"	union all" +
					"	select "+y+" as year,"+m+" as month," +
					"	a.kpi_id,a.item_name,a.display_name,a.point,a.target_value,a.description,a.frequence,a.target_type," +
					"	a.standard_type,a.detail_type,a.different_scale,a.add_type,a.scale_point,a.formula,a.default_value," +
					"	a.section_detail,a.danwei,a.kaohe_id," +
					"	b.kpi_detail_id as c_id,b.item_name as c_item_name,b.display_name as c_display_name," +
					"	b.target_value as c_target_value,b.default_value as c_default_value,b.target_type as c_target_type," +
					"	b.different_scale as c_different_scale,b.add_type as c_add_type,b.scale_point as c_scale_point," +
					"	b.formula as c_formula,b.no_double as c_no_double,b.danwei as c_danwei," +
					"	1 as kpi_detail_id,b.guikou_id from (" +
					"	select KPI_VS_ORGANIZATION.*,KPI_DETAIL_VS_ORGANIZATION.kpi_detail_id from KPI_VS_ORGANIZATION,KPI_DETAIL_VS_ORGANIZATION" +
					"	where KPI_VS_ORGANIZATION.kaohe_id = KPI_DETAIL_VS_ORGANIZATION.kaohe_id" +
					"	and KPI_VS_ORGANIZATION.guikou_id = -1" +
					"	and KPI_VS_ORGANIZATION.kpi_id = KPI_DETAIL_VS_ORGANIZATION.parent_id" +
					"	and KPI_VS_ORGANIZATION.kaohe_id = " +kaohe_id+
					(replaceGuikou.equals("0")?"":"	and KPI_DETAIL_VS_ORGANIZATION.guikou_id in (" +replaceGuikou+")")+
					"	) as a" +
					"	left join KPI_DETAIL_VS_ORGANIZATION as b" +
					"	on a.kpi_id = b.parent_id" +
					"	and a.kaohe_id = b.kaohe_id" +
					"	and a.kpi_detail_id = b.kpi_detail_id"+
					
					
//					"   from" +
//					"   (" +
//					"	select "+y+" as year,"+m+" as month,pk.id as kpi_id,pk.item_name,pk.display_name,pk.point,pk.target_value," +
//					"	pk.description,pk.frequence,pk.target_type,pk.standard_type,pk.detail_type," +
//					"	pk.different_scale,pk.add_type,pk.scale_point,pk.formula,pk.default_value," +
//					"	pk.section_detail,pk.danwei,pk.kaohe_id,pk.guikou_id," +
//					"	kd.id as c_id,kd.item_name as c_item_name,kd.display_name as c_display_name," +
//					"	kd.target_value as c_target_value,kd.default_value as c_default_value,kd.target_type as c_target_type," +
//					"	kd.different_scale as c_different_scale,kd.add_type as c_add_type,kd.scale_point as c_scale_point," +
//					"	kd.formula as c_formula,kd.no_double as c_no_double,kd.danwei as c_danwei," +
//					"	case when pk.detail_type = 2 then" +
//					"	(select count(1) from KPI_VS_ORGANIZATION as t" +
//					"	where t.kaohe_id = pk.kaohe_id and t.guikou_id = pk.guikou_id and pk.id = pk.id and t.kpi_detail_id = kd.id)" +
//					"	else 1 end as kpi_detail_id  " +
//					"	from" +
//					"	(" +
//					"	select * from DICT_KPI_ITEM as dk,	KPI_VS_ORGANIZATION as ko" +
//					"	where dk.id = ko.kpi_id" +
//					(replaceGuikou.equals("0")?"":"	and ko.guikou_id in (" +replaceGuikou+")")+
////					(guikou_ids.equals("0")?"":"	and ko.guikou_id in( " +guikou_ids+")")+
//					"	and ko.kaohe_id = " +kaohe_id+
//					"	) as pk" +
//					"	left join  DICT_KPI_ITEM_DETAIL as kd" +
//					"	on kd.id = pk.kpi_detail_id" +
					
					
					
					"   ) as m" +
					"   left join KPI_VALUE as kv" +
					"   on" +
					"   m.kpi_id = kv.kpi_id" +
					"   and kv.year = m.year and kv.month = m.month" +
//					"   and kv.guikou_id = m.guikou_id" +
					"	and kv.kaohe_id = m.kaohe_id " +
					"   left join KPI_DETAIL_VALUE as kdv" +
					"   on" +
					"   kdv.year = m.year and kdv.month = m.month" +
					"   and kdv.kpi_detail_id = m.c_id" +
					"   and kdv.guikou_id = m.guikou_id" +
					"   and kdv.kaohe_id = m.kaohe_id" +
					"   order by standard_type,detail_type,display_name";
				

			ResultSet rs = queryData(conn,sql);
			List<KpiValueVo> rtList = new ArrayList();
			KpiValueVo vo ;
			while(rs.next()){
				vo = new KpiValueVo();
				
				vo.is_over_due = isOverDue;
				
				vo.is_my_detail = rs.getInt("kpi_detail_id");
				vo.kpi_id	=	rs.getInt("kpi_id");
				vo.kpi_name	=	rs.getString("item_name");
				vo.display_name	=	rs.getString("display_name");
				vo.point	=	DataFormater.numberToString(rs.getBigDecimal("point"));
				vo.target_value	=	DataFormater.numberToString(rs.getBigDecimal("target_value"));
				vo.description	=	rs.getString("description");
				vo.frequence	=	rs.getString("frequence");
				vo.target_type	=	rs.getInt("target_type");
				vo.standard_type	=	rs.getInt("standard_type");
				vo.detail_type	=	rs.getInt("detail_type");
				vo.different_scale	=	DataFormater.numberToString(rs.getBigDecimal("different_scale"));
				vo.add_type	=	rs.getInt("add_type");
				vo.scale_point	=	DataFormater.numberToString(rs.getBigDecimal("scale_point"));
				vo.formula	=	rs.getString("formula");
				vo.default_value	=	rs.getString("default_value");
				vo.section_detail	=	rs.getString("section_detail");
				vo.danwei	=	rs.getInt("danwei");
				vo.kaohe_id	=	rs.getInt("kaohe_id");
				vo.kaohe_name	=	rs.getString("kaohe_name");
				vo.guikou_id	=	rs.getInt("guikou_id");
				vo.guikou_name	=	rs.getString("guikou_name");
				vo.detail = rs.getString("detail");
				
				vo.value	=	DataFormater.numberToString(rs.getBigDecimal("value"));
				if(vo.value != null && !vo.value.equals(""))
				{
					vo.tmp_value = vo.value;
				}
				else
				{
					vo.tmp_value = vo.default_value;
				}
				vo.create_user	=	rs.getString("create_user");
				vo.create_time	=	rs.getDate("create_time");
				vo.score	=	rs.getString("score");
				vo.update_score	=	DataFormater.numberToString(rs.getBigDecimal("update_score"));
				vo.tmp_update_score	=	vo.update_score;
				vo.update_user	=	rs.getString("update_user");
				vo.update_time	=	rs.getDate("update_time");
				vo.update_description	=	rs.getString("update_description");
				vo.valid	=	rs.getInt("valid");
				vo.tmp_valid	=	vo.valid;
				vo.valid_detail = rs.getString("valid_detail");
				vo.valid_user	=	rs.getString("valid_user");
				vo.valid_time	=	rs.getDate("valid_time");
				
				vo.v_id = rs.getInt("v_id");
				vo.vc_id = rs.getInt("vc_id");
				vo.c_id	= rs.getInt("c_id");
				vo.c_item_name	=	rs.getString("c_item_name");
				vo.c_display_name	=	rs.getString("c_display_name");
				vo.c_target_value	=	DataFormater.numberToString(rs.getBigDecimal("c_target_value"));
				vo.c_default_value	=	DataFormater.numberToString(rs.getBigDecimal("c_default_value"));
				vo.c_target_type	=	rs.getInt("c_target_type");
				vo.c_different_scale	=	DataFormater.numberToString(rs.getBigDecimal("c_different_scale"));
				vo.c_add_type	=	rs.getInt("c_add_type");
				vo.c_scale_point	=	DataFormater.numberToString(rs.getBigDecimal("c_scale_point"));
				vo.c_formula	=	rs.getString("c_formula");
				vo.c_no_double	=	rs.getInt("c_no_double");
				vo.c_danwei	=	rs.getInt("c_danwei");
//				vo.id	=	rs.getInt("id");
				vo.year	=	rs.getInt("year");
				vo.month	=	rs.getInt("month");				
				vo.c_id	=	rs.getInt("c_id");
				vo.c_value	=	DataFormater.numberToString(rs.getBigDecimal("c_value"));
				if(vo.c_value != null && !vo.c_value.equals(""))
				{
					vo.c_tmp_value = vo.c_value;
				}
				else
				{
					vo.c_tmp_value = vo.c_target_value;
				}
				vo.c_score	=	DataFormater.numberToString(rs.getBigDecimal("c_score"));
				vo.c_create_user	=	rs.getString("c_create_user");
				vo.c_create_time	=	rs.getDate("c_create_time");
				vo.c_update_score	=	DataFormater.numberToString(rs.getBigDecimal("c_update_score"));
				vo.c_tmp_update_score	=	vo.c_update_score;
				vo.c_update_user	=	rs.getString("c_update_user");
				vo.c_update_time	=	rs.getDate("c_update_time");
				vo.c_valid	=	rs.getInt("c_valid");
				vo.c_valid_user	=	rs.getString("c_valid_user");
				vo.c_valid_time	=	rs.getDate("c_valid_time");
				vo.c_detail = rs.getString("c_detail");
//				System.out.println(">>>>>>>>>>>>>>>>>>>>>"+vo.score);
				rtList.add(vo);
			}
			freeConnection(conn);
			return rtList;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public List<KpiValueVo> getKpiPointByGuikouKaoheForShow(String guikou_ids,int kaohe_id,int y,int m)
	{
		List<KpiValueVo> list = getKpiPointByGuikouKaohe( guikou_ids, kaohe_id, y, m);
		list = calParentScore(list);//将每个明细项目的score都赋上值
		int kpi_id = 0;		
		for(int i = 0;i<list.size();i++)//接着增加明细项目的父项目
		{
			KpiValueVo ovo = list.get(i);
			if(ovo.detail_type == 2)//明细
			{
				if(ovo.kpi_id == kpi_id)
				{
					continue;
				}
				kpi_id = ovo.kpi_id;
				KpiValueVo vo = new KpiValueVo();
				vo.kpi_id	=	ovo.kpi_id;
				vo.kpi_name	=	ovo.kpi_name;
				vo.display_name	=	ovo.display_name;
				vo.point	=	ovo.point;
				vo.score = ovo.score;
				vo.target_value	=	ovo.target_value	;
				vo.description	=	ovo.description;
				vo.frequence	=	ovo.frequence;
				vo.target_type	=	ovo.target_type;
				vo.standard_type	=	ovo.standard_type;
				vo.detail_type	=	ovo.detail_type;
				vo.different_scale	=	ovo.different_scale;
				vo.add_type	=	ovo.add_type;
				vo.scale_point	=	ovo.scale_point;
				vo.formula	=	ovo.formula;
				vo.default_value	=	ovo.default_value;
				vo.section_detail	=	ovo.section_detail;
				vo.danwei	=	ovo.danwei;
				vo.kaohe_id	=	ovo.kaohe_id;
				vo.kaohe_name	=	ovo.kaohe_name;
				vo.guikou_id	=	ovo.guikou_id;
				vo.guikou_name	=	ovo.guikou_name;
				
				vo.update_score = ovo.update_score;
				vo.tmp_update_score = vo.update_score;
				
				vo.year = ovo.year;
				vo.month = ovo.month;
				
				
				int x = 0;
				for(KpiValueVo tmpVo:list)
				{
					if(tmpVo.year == vo.year
						&& tmpVo.month == vo.month
						&& tmpVo.kpi_id == vo.kpi_id
						&& tmpVo.kaohe_id == vo.kaohe_id
						&& tmpVo.c_id != 0
						)
					{
						x ++;
					}
				}
//				vo.v_id = ovo.v_id;
				vo.v_id = x;
				
				vo.detail = ovo.detail;
				vo.valid = ovo.valid;
				vo.tmp_valid = ovo.valid;				
				vo.valid_detail = ovo.valid_detail;
				list.add(i, vo);
			}			
		}
		return list;	
	}
	public List<KpiValueVo> getKpiPointByGuikouItemForShow(String guikou_ids,int kpi_id,int y,int m,Connection conn) 
	{
		List<KpiValueVo> list = getKpiPointByGuikouItem( guikou_ids, kpi_id, y, m,conn);
		//计算父的分数(此时每条都是明细，父节点尚未生成，只是每条明细都包含父节点的内容)
		list = calParentScore(list,conn);
		//增加父节点
		int kaohe_id = 0;
		for(int i = 0;i<list.size();i++)
		{
			KpiValueVo ovo = list.get(i);
//			System.out.println(ovo.kpi_name+"   "+kaohe_id+"  "+ovo.kpi_id+" "+ovo.c_id);
			if(ovo.detail_type == 2)//明细
			{
				if(ovo.kaohe_id == kaohe_id)
				{
					continue;
				}
				kaohe_id = ovo.kaohe_id;
				KpiValueVo vo = new KpiValueVo();			
				vo.kpi_id	=	ovo.kpi_id;
				vo.kpi_name	=	ovo.kpi_name;
				vo.display_name	=	ovo.display_name;
				vo.point	=	ovo.point;
				vo.score = ovo.score;
				vo.target_value	=	ovo.target_value	;
				vo.description	=	ovo.description;
				vo.frequence	=	ovo.frequence;
				vo.target_type	=	ovo.target_type;
				vo.standard_type	=	ovo.standard_type;
				vo.detail_type	=	ovo.detail_type;
				vo.different_scale	=	ovo.different_scale;
				vo.add_type	=	ovo.add_type;
				vo.scale_point	=	ovo.scale_point;
				vo.formula	=	ovo.formula;
				vo.default_value	=	ovo.default_value;
				vo.section_detail	=	ovo.section_detail;
				vo.danwei	=	ovo.danwei;
				vo.kaohe_id	=	ovo.kaohe_id;
				vo.kaohe_name	=	ovo.kaohe_name;
				vo.guikou_id	=	-1;//ovo.guikou_id;
				vo.guikou_name	=	null;//ovo.guikou_name;
				vo.year = ovo.year;
				vo.month = ovo.month;
				vo.v_id = ovo.v_id;
				vo.update_score = ovo.update_score;
				vo.tmp_update_score = vo.update_score;
				
				vo.detail = ovo.detail;
				vo.valid = ovo.valid;
				vo.tmp_valid = ovo.valid;				
				vo.valid_detail = ovo.valid_detail;
				list.add(i, vo);
			}			
		}
		return list;
	}

	public List<KpiValueVo> getKpiPointByGuikouItemForShow(String guikou_ids,int kpi_id,int y,int m) 
	{
		List<KpiValueVo> list = getKpiPointByGuikouItem( guikou_ids, kpi_id, y, m);
		//计算父的分数(此时每条都是明细，父节点尚未生成，只是每条明细都包含父节点的内容)
		list = calParentScore(list);
		//增加父节点
		int kaohe_id = 0;
		for(int i = 0;i<list.size();i++)
		{
			KpiValueVo ovo = list.get(i);
//			System.out.println(ovo.kpi_name+"   "+kaohe_id+"  "+ovo.kpi_id+" "+ovo.c_id);
			if(ovo.detail_type == 2)//明细
			{
				if(ovo.kaohe_id == kaohe_id)
				{
					continue;
				}
				kaohe_id = ovo.kaohe_id;
				KpiValueVo vo = new KpiValueVo();			
				vo.kpi_id	=	ovo.kpi_id;
				vo.kpi_name	=	ovo.kpi_name;
				vo.display_name	=	ovo.display_name;
				vo.point	=	ovo.point;
				vo.score = ovo.score;
				vo.target_value	=	ovo.target_value	;
				vo.description	=	ovo.description;
				vo.frequence	=	ovo.frequence;
				vo.target_type	=	ovo.target_type;
				vo.standard_type	=	ovo.standard_type;
				vo.detail_type	=	ovo.detail_type;
				vo.different_scale	=	ovo.different_scale;
				vo.add_type	=	ovo.add_type;
				vo.scale_point	=	ovo.scale_point;
				vo.formula	=	ovo.formula;
				vo.default_value	=	ovo.default_value;
				vo.section_detail	=	ovo.section_detail;
				vo.danwei	=	ovo.danwei;
				vo.kaohe_id	=	ovo.kaohe_id;
				vo.kaohe_name	=	ovo.kaohe_name;
				vo.guikou_id	=	-1;//ovo.guikou_id;
				vo.guikou_name	=	null;//ovo.guikou_name;
				vo.year = ovo.year;
				vo.month = ovo.month;
				vo.v_id = ovo.v_id;
				vo.update_score = ovo.update_score;
				vo.tmp_update_score = vo.update_score;
				
				vo.detail = ovo.detail;
				vo.valid = ovo.valid;
				vo.tmp_valid = ovo.valid;				
				vo.valid_detail = ovo.valid_detail;
				list.add(i, vo);
			}			
		}
		for(int i = 0;i< list.size();i++)
		{
//			System.out.println(list.get(i).kpi_name+"   "+list.get(i).c_id);
		}
		return list;
	}
	/*
	 * save list full point
	*/
	public List<KpiValueVo> saveListFullPoint(List<KpiValueVo> l,String user_id,int view_type) {
		Connection conn = getConnect();
		try
		{
			conn.setAutoCommit(false);
			for(KpiValueVo kv:l){
				if(kv.detail_type == 2){
					System.out.println("2  :  "+kv.description);
					kv.c_tmp_value = kv.c_target_value;
				}else{
					System.out.println("qt  :  "+kv.description);
					kv.tmp_value = kv.target_value;
				}
			}
			List<KpiValueVo> newL = checkPoint(l,user_id,conn);
			int kaohe_id = 0;
			int guikou_id = 0;
			int kpi_id = 0;
			int year = 0;
			int month = 0;
			for(KpiValueVo vo :newL)
			{
				kpi_id = vo.kpi_id;
				kaohe_id = vo.kaohe_id;
				guikou_id = vo.guikou_id;
				int v_id = vo.v_id;
				int vc_id = vo.vc_id;
				year = vo.year;
				month = vo.month;
				String formula = vo.formula;
				int standard_type = vo.standard_type;
				int detail_type = vo.detail_type;
				int c_id = vo.c_id;
				int no_double = vo.c_no_double;
				String tmp_value = vo.tmp_value;
				if(tmp_value == null || "".equals(tmp_value))
				{
					tmp_value = "0";
				}
				String c_tmp_value = vo.c_tmp_value;
				if(c_tmp_value == null || "".equals(c_tmp_value))
				{
					c_tmp_value = "0";
				}
				String score = vo.score;
				String c_score = vo.c_score;
				String sql = "";
				
								
				if(detail_type == 2 && c_id != 0)//明细
				{
//					if(vo.is_my_detail == 0)
//					{
//						continue;
//					}
					String detail = (vo.c_detail!=null && !"".equals(vo.c_detail))?("'"+vo.c_detail.replaceAll("'", "''")+"'"):"NULL";
					if(vc_id == 0)//插入
					{
						
						int kpi_detail_id = vo.c_id;					
						int newId = getNewMaxId(conn,"KPI_DETAIL_VALUE");
						if(c_score == ""){
							System.out.println(".....");
						}
						sql = "insert into KPI_DETAIL_VALUE(id,kpi_detail_id,year,month,kaohe_id,guikou_id,	" +
								" value,score," +
								" create_user,create_time,update_score,update_user,update_time," +
								" valid,valid_user,valid_time,update_description,detail,parent_id)" +
								" values("+newId+","+kpi_detail_id+","+year+","+month+","+kaohe_id+","+guikou_id+
								"," +c_tmp_value+","+c_score+","+
								"'"+user_id+"','"+DateTools.dtsWithTime(new Date())+"',NULL,NULL,NULL,NULL,NULL,NULL,NULL,"+detail+","+kpi_id+")";//201303+parent_id
						updateData(conn, sql);		
					}
					else
					{
						sql = "update KPI_DETAIL_VALUE set value= "+c_tmp_value+",score=" +c_score+",detail ="+detail+
						" where id = " +vc_id;
						updateData(conn, sql);	
					}
						
				}
				else if(detail_type == 2 && c_id == 0)//父
				{
					String detail = (vo.detail!=null && !"".equals(vo.detail))?("'"+vo.detail.replaceAll("'", "''")+"'"):"NULL";
//					if(v_id == 0)//插入
//					{
						sql = "select count(1) from KPI_VALUE where kpi_id="+vo.kpi_id+" and kaohe_id = "+vo.kaohe_id;
						ResultSet rs = queryData(conn, sql);
						if(rs.next())
						{
							if(rs.getInt(1) > 0)
							{
								sql = "update KPI_VALUE set value = "+tmp_value+",score = "+score+",detail = "+detail+
									" where kpi_id="+vo.kpi_id+" and kaohe_id = "+vo.kaohe_id;
								updateData(conn, sql);
							}
							else
							{
								int newId = getNewMaxId(conn,"KPI_VALUE");
								sql = "insert into KPI_VALUE(id,year,month,kpi_id,kaohe_id,guikou_id," +
										"create_user,create_time,value,score,update_score," +
										"update_user,update_time,update_description,valid,valid_user,valid_time,detail)" +
										"values("+newId+","+year+","+month+","+kpi_id+","+kaohe_id+","+guikou_id+"," +
										"'"+user_id+"','"+DateTools.dtsWithTime(new Date())+"',"+tmp_value+","+score+",NULL," +
										"NULL,NULL,NULL,0,NULL,NULL,"+detail+")";
								updateData(conn, sql);			
							}
						}
							
//					}
//					else
//					{	
////						int newId = getNewMaxId(conn,"KPI_VALUE");
////						sql = "insert into KPI_VALUE(id,year,month,kpi_id,kaohe_id,guikou_id," +
////								"create_user,create_time,value,score,update_score," +
////								"update_user,update_time,update_description,valid,valid_user,valid_time,detail)" +
////								"values("+newId+","+year+","+month+","+kpi_id+","+kaohe_id+",-1," +
////								"'"+user_id+"','"+DateTools.dtsWithTime(new Date())+"',"+tmp_value+","+score+",NULL," +
////								"NULL,NULL,NULL,0,NULL,NULL,"+detail+")";
////						updateData(conn, sql);		
//						sql = "update KPI_VALUE set value = "+tmp_value+",score = "+score+",detail = "+detail+
//							" where kpi_id="+vo.kpi_id+" and kaohe_id = "+vo.kaohe_id;
//						updateData(conn, sql);
//					}
//					updateOtherGuikouScore(year, month, kpi_id, kaohe_id, guikou_id, score, conn);
				}
				else
				{
					String detail = (vo.detail!=null && !"".equals(vo.detail))?("'"+vo.detail.replaceAll("'", "''")+"'"):"NULL";
					if(v_id == 0 && !"".equals(tmp_value))//插入 改成0也可保存
//					if(v_id == 0 && !"".equals(tmp_value) && !"0".equals(tmp_value))//插入
					{						
						int newId = getNewMaxId(conn,"KPI_VALUE");
						sql = "insert into KPI_VALUE(id,year,month,kpi_id,kaohe_id,guikou_id," +
								"create_user,create_time,value,score,update_score," +
								"update_user,update_time,update_description,valid,valid_user,valid_time,detail)" +
								"values("+newId+","+year+","+month+","+kpi_id+","+kaohe_id+","+guikou_id+"," +
								"'"+user_id+"','"+DateTools.dtsWithTime(new Date())+"',"+tmp_value+","+score+",NULL," +
								"NULL,NULL,NULL,0,NULL,NULL,"+detail+")";
						updateData(conn, sql);
					}
					else
					{
						sql = "update KPI_VALUE set value = "+tmp_value+",score = "+score+",detail ="+detail+
								" where id = "+v_id;
						updateData(conn, sql);
					}					
				}				
			}
			conn.commit();
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			try{
				if(conn != null){
					conn.rollback();//回滚。
					freeConnection(conn);
				}
			}catch(Exception ex2){
				ex2.printStackTrace();
			}
		}finally{
			try{
				if(conn != null){
					conn.setAutoCommit(true);
					freeConnection(conn);
				}
			}catch(Exception ex2){
				ex2.printStackTrace();
			}
		}
		freeConnection(conn);
		return l;
	}
	
	
	public List<KpiValueVo> saveListFullPoint(List<KpiValueVo> l,String user_id,int view_type,Connection conn) {
		try
		{
			conn.setAutoCommit(false);
			for(KpiValueVo kv:l){
				if(kv.detail_type == 2){
					System.out.println("2  :  "+kv.description);
					kv.c_tmp_value = kv.c_target_value;
				}else{
					System.out.println("qt  :  "+kv.description);
					kv.tmp_value = kv.target_value;
				}
			}
			List<KpiValueVo> newL = checkPoint(l,user_id,conn);
			int kaohe_id = 0;
			int guikou_id = 0;
			int kpi_id = 0;
			int year = 0;
			int month = 0;
			for(KpiValueVo vo :newL)
			{
				kpi_id = vo.kpi_id;
				kaohe_id = vo.kaohe_id;
				guikou_id = vo.guikou_id;
				int v_id = vo.v_id;
				int vc_id = vo.vc_id;
				year = vo.year;
				month = vo.month;
				String formula = vo.formula;
				int standard_type = vo.standard_type;
				int detail_type = vo.detail_type;
				int c_id = vo.c_id;
				int no_double = vo.c_no_double;
				String tmp_value = vo.tmp_value;
				if(tmp_value == null || "".equals(tmp_value))
				{
					tmp_value = "0";
				}
				String c_tmp_value = vo.c_tmp_value;
				if(c_tmp_value == null || "".equals(c_tmp_value))
				{
					c_tmp_value = "0";
				}
				String score = vo.score;
				String c_score = vo.c_score;
				String sql = "";
				
								
				if(detail_type == 2 && c_id != 0)//明细
				{
					String detail = (vo.c_detail!=null && !"".equals(vo.c_detail))?("'"+vo.c_detail.replaceAll("'", "''")+"'"):"NULL";
					if(vc_id == 0)//插入
					{
						
						int kpi_detail_id = vo.c_id;					
						int newId = getNewMaxId(conn,"KPI_DETAIL_VALUE");
						if(c_score == ""){
							System.out.println(".....");
						}
						sql = "insert into KPI_DETAIL_VALUE(id,kpi_detail_id,year,month,kaohe_id,guikou_id,	" +
								" value,score," +
								" create_user,create_time,update_score,update_user,update_time," +
								" valid,valid_user,valid_time,update_description,detail,parent_id)" +
								" values("+newId+","+kpi_detail_id+","+year+","+month+","+kaohe_id+","+guikou_id+
								"," +c_tmp_value+","+c_score+","+
								"'"+user_id+"','"+DateTools.dtsWithTime(new Date())+"',NULL,NULL,NULL,NULL,NULL,NULL,NULL,"+detail+","+kpi_id+")";//201303+parent_id
						updateData(conn, sql);		
					}
					else
					{
						sql = "update KPI_DETAIL_VALUE set value= "+c_tmp_value+",score=" +c_score+",detail ="+detail+
						" where id = " +vc_id;
						updateData(conn, sql);	
					}
						
				}
				else if(detail_type == 2 && c_id == 0)//父
				{
					String detail = (vo.detail!=null && !"".equals(vo.detail))?("'"+vo.detail.replaceAll("'", "''")+"'"):"NULL";
//					if(v_id == 0)//插入
//					{
						sql = "select count(1) from KPI_VALUE where kpi_id="+vo.kpi_id+" and kaohe_id = "+vo.kaohe_id;
						ResultSet rs = queryData(conn, sql);
						if(rs.next())
						{
							if(rs.getInt(1) > 0)
							{
								sql = "update KPI_VALUE set value = "+tmp_value+",score = "+score+",detail = "+detail+
									" where kpi_id="+vo.kpi_id+" and kaohe_id = "+vo.kaohe_id;
								updateData(conn, sql);
							}
							else
							{
								int newId = getNewMaxId(conn,"KPI_VALUE");
								sql = "insert into KPI_VALUE(id,year,month,kpi_id,kaohe_id,guikou_id," +
										"create_user,create_time,value,score,update_score," +
										"update_user,update_time,update_description,valid,valid_user,valid_time,detail)" +
										"values("+newId+","+year+","+month+","+kpi_id+","+kaohe_id+","+guikou_id+"," +
										"'"+user_id+"','"+DateTools.dtsWithTime(new Date())+"',"+tmp_value+","+score+",NULL," +
										"NULL,NULL,NULL,0,NULL,NULL,"+detail+")";
								updateData(conn, sql);			
							}
						}
				}
				else
				{
					String detail = (vo.detail!=null && !"".equals(vo.detail))?("'"+vo.detail.replaceAll("'", "''")+"'"):"NULL";
					if(v_id == 0 && !"".equals(tmp_value))//插入 改成0也可保存
//					if(v_id == 0 && !"".equals(tmp_value) && !"0".equals(tmp_value))//插入
					{						
						int newId = getNewMaxId(conn,"KPI_VALUE");
						sql = "insert into KPI_VALUE(id,year,month,kpi_id,kaohe_id,guikou_id," +
								"create_user,create_time,value,score,update_score," +
								"update_user,update_time,update_description,valid,valid_user,valid_time,detail)" +
								"values("+newId+","+year+","+month+","+kpi_id+","+kaohe_id+","+guikou_id+"," +
								"'"+user_id+"','"+DateTools.dtsWithTime(new Date())+"',"+tmp_value+","+score+",NULL," +
								"NULL,NULL,NULL,0,NULL,NULL,"+detail+")";
						updateData(conn, sql);
					}
					else
					{
						sql = "update KPI_VALUE set value = "+tmp_value+",score = "+score+",detail ="+detail+
								" where id = "+v_id;
						updateData(conn, sql);
					}					
				}				
			}
			conn.commit();
			conn.setAutoCommit(true);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			try{
				if(conn != null){
					conn.rollback();//回滚。
					conn.setAutoCommit(true);
				}
			}catch(Exception ex2){
				ex2.printStackTrace();
			}
		}finally{
			try{
				if(conn != null){
					conn.setAutoCommit(true);
				}
			}catch(Exception ex2){
				ex2.printStackTrace();
			}
		}
		
		return l;
	}
	/*
	 * save my all evaluate item full point
	*/
	public void saveAllFullPoint(String guishu_id,String user_id,int y,int m) {
		try{
			Connection conn = getConnect();
			List<KpiDictItemVsOrganizationVo> l = getKpiByGuikou(guishu_id,conn) ;
			for(KpiDictItemVsOrganizationVo vo:l){
				List<KpiValueVo> l2 = getKpiPointByGuikouItemForShow(guishu_id,vo.id,y,m,conn);
				saveListFullPoint(l2, user_id,0,conn);
			}
			freeConnection(conn);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
	}
	
	/**
	 * save all point
	 * 
	 * **/

	public List<KpiValueVo> savePoint(List<KpiValueVo> l,String user_id,int view_type) {
		Connection conn = getConnect();
		try
		{
			conn.setAutoCommit(false);
			List<KpiValueVo> newL = checkPoint(l,user_id,conn);
			int kaohe_id = 0;
			int guikou_id = 0;
			int kpi_id = 0;
			int year = 0;
			int month = 0;
			for(KpiValueVo vo :newL)
			{
				kpi_id = vo.kpi_id;
				kaohe_id = vo.kaohe_id;
				guikou_id = vo.guikou_id;
				int v_id = vo.v_id;
				int vc_id = vo.vc_id;
				year = vo.year;
				month = vo.month;
				String formula = vo.formula;
				int standard_type = vo.standard_type;
				int detail_type = vo.detail_type;
				int c_id = vo.c_id;
				int no_double = vo.c_no_double;
				String tmp_value = vo.tmp_value;
				if(tmp_value == null || "".equals(tmp_value))
				{
					tmp_value = "0";
				}
				String c_tmp_value = vo.c_tmp_value;
				if(c_tmp_value == null || "".equals(c_tmp_value))
				{
					c_tmp_value = "0";
				}
				String score = vo.score;
				String c_score = vo.c_score;
				String sql = "";
				
								
				if(detail_type == 2 && c_id != 0)//明细
				{
//					if(vo.is_my_detail == 0)
//					{
//						continue;
//					}
					String detail = (vo.c_detail!=null && !"".equals(vo.c_detail))?("'"+vo.c_detail.replaceAll("'", "''")+"'"):"NULL";
					if(vc_id == 0)//插入
					{
						
						int kpi_detail_id = vo.c_id;					
						int newId = getNewMaxId(conn,"KPI_DETAIL_VALUE");
						sql = "insert into KPI_DETAIL_VALUE(id,kpi_detail_id,year,month,kaohe_id,guikou_id,	" +
								" value,score," +
								" create_user,create_time,update_score,update_user,update_time," +
								" valid,valid_user,valid_time,update_description,detail,parent_id)" +
								" values("+newId+","+kpi_detail_id+","+year+","+month+","+kaohe_id+","+guikou_id+
								"," +c_tmp_value+","+c_score+","+
								"'"+user_id+"','"+DateTools.dtsWithTime(new Date())+"',NULL,NULL,NULL,NULL,NULL,NULL,NULL,"+detail+","+kpi_id+")";//201303+parent_id
						updateData(conn, sql);		
					}
					else
					{
						sql = "update KPI_DETAIL_VALUE set value= "+c_tmp_value+",score=" +c_score+",detail ="+detail+
						" where id = " +vc_id;
						updateData(conn, sql);	
					}
						
				}
				else if(detail_type == 2 && c_id == 0)//父
				{
					String detail = (vo.detail!=null && !"".equals(vo.detail))?("'"+vo.detail.replaceAll("'", "''")+"'"):"NULL";
//					if(v_id == 0)//插入
//					{
						sql = "select count(1) from KPI_VALUE where kpi_id="+vo.kpi_id+" and kaohe_id = "+vo.kaohe_id;
						ResultSet rs = queryData(conn, sql);
						if(rs.next())
						{
							if(rs.getInt(1) > 0)
							{
								sql = "update KPI_VALUE set value = "+tmp_value+",score = "+score+",detail = "+detail+
									" where kpi_id="+vo.kpi_id+" and kaohe_id = "+vo.kaohe_id;
								updateData(conn, sql);
							}
							else
							{
								int newId = getNewMaxId(conn,"KPI_VALUE");
								sql = "insert into KPI_VALUE(id,year,month,kpi_id,kaohe_id,guikou_id," +
										"create_user,create_time,value,score,update_score," +
										"update_user,update_time,update_description,valid,valid_user,valid_time,detail)" +
										"values("+newId+","+year+","+month+","+kpi_id+","+kaohe_id+","+guikou_id+"," +
										"'"+user_id+"','"+DateTools.dtsWithTime(new Date())+"',"+tmp_value+","+score+",NULL," +
										"NULL,NULL,NULL,0,NULL,NULL,"+detail+")";
								updateData(conn, sql);			
							}
						}
							
//					}
//					else
//					{	
////						int newId = getNewMaxId(conn,"KPI_VALUE");
////						sql = "insert into KPI_VALUE(id,year,month,kpi_id,kaohe_id,guikou_id," +
////								"create_user,create_time,value,score,update_score," +
////								"update_user,update_time,update_description,valid,valid_user,valid_time,detail)" +
////								"values("+newId+","+year+","+month+","+kpi_id+","+kaohe_id+",-1," +
////								"'"+user_id+"','"+DateTools.dtsWithTime(new Date())+"',"+tmp_value+","+score+",NULL," +
////								"NULL,NULL,NULL,0,NULL,NULL,"+detail+")";
////						updateData(conn, sql);		
//						sql = "update KPI_VALUE set value = "+tmp_value+",score = "+score+",detail = "+detail+
//							" where kpi_id="+vo.kpi_id+" and kaohe_id = "+vo.kaohe_id;
//						updateData(conn, sql);
//					}
//					updateOtherGuikouScore(year, month, kpi_id, kaohe_id, guikou_id, score, conn);
				}
				else
				{
					String detail = (vo.detail!=null && !"".equals(vo.detail))?("'"+vo.detail.replaceAll("'", "''")+"'"):"NULL";
					if(v_id == 0 && !"".equals(tmp_value))//插入 改成0也可保存
//					if(v_id == 0 && !"".equals(tmp_value) && !"0".equals(tmp_value))//插入
					{						
						int newId = getNewMaxId(conn,"KPI_VALUE");
						sql = "insert into KPI_VALUE(id,year,month,kpi_id,kaohe_id,guikou_id," +
								"create_user,create_time,value,score,update_score," +
								"update_user,update_time,update_description,valid,valid_user,valid_time,detail)" +
								"values("+newId+","+year+","+month+","+kpi_id+","+kaohe_id+","+guikou_id+"," +
								"'"+user_id+"','"+DateTools.dtsWithTime(new Date())+"',"+tmp_value+","+score+",NULL," +
								"NULL,NULL,NULL,0,NULL,NULL,"+detail+")";
						updateData(conn, sql);
					}
					else
					{
						sql = "update KPI_VALUE set value = "+tmp_value+",score = "+score+",detail ="+detail+
								" where id = "+v_id;
						updateData(conn, sql);
					}					
				}				
			}
			conn.commit();
			if(view_type == 0)
			{
//				return getKpiPointByGuikouItemForShow(guikou_id,kpi_id,year,month);
			}
			else
			{
//				return getKpiPointByGuikouKaoheForShow(guikou_id,kaohe_id,year,month);
			}
			
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			try{
				if(conn != null){
					conn.rollback();//回滚。
				}
			}catch(Exception ex2){
				ex2.printStackTrace();
			}
		}finally{
			try{
				if(conn != null){
					conn.setAutoCommit(true);
				}
			}catch(Exception ex2){
				ex2.printStackTrace();
			}
		}
		return l;
	}
	
	public List<KpiValueVo> saveValid(List<KpiValueVo> l,String user_id,int view_type) {
		Connection conn = getConnect();	
		try
		{			
			conn.setAutoCommit(false);
//			int kpi_id = 0;
//			int kaohe_id = 0;
			String tmp_update_score = "";
			int valid = 0;//如果是明细，一定是父先更新，然后子再用。
			int year = 0;
			int month = 0;
			for(KpiValueVo vo :l)
			{
				if(vo.valid == 1)//已经验证，无需更改
				{
					continue;
				}
				if(vo.v_id == 0)//尚未提交数据，无需更改
				{
					continue;
				}
				
				int v_id = vo.v_id;
				year = vo.year;
				month = vo.month;	
				
				
				String valid_detail = (vo.valid_detail!=null && !"".equals(vo.valid_detail))?("'"+vo.valid_detail.replaceAll("'", "''")+"'"):"NULL";
				
				if(vo.detail_type != 2)//非明细数据，都当作父考虑
				{
//					System.out.println(year+"年"+month+"月   审核：    valid="+vo.valid+"  tmp_valid="+vo.tmp_valid+"  score="+vo.score+"   update_score:"+vo.update_score+"   tmp_update_score:"+vo.tmp_update_score);
					valid = vo.tmp_valid;
					String sql ;
					if(vo.tmp_update_score == "")//没有临时数据，不需要修改tmp_update_score
					{
						sql = "update KPI_VALUE " +
							"set " +
							"valid = "+valid+
							",valid_user = '"+user_id+"'"+
							",valid_time = '"+DateTools.dtsWithTime(new Date())+"'"+
							",valid_detail = "+valid_detail+""+
							" where id = " +v_id;						
					}
					else
					{
						sql = "update KPI_VALUE " +
							"set " +
							"valid = "+valid+
							",valid_user = '"+user_id+"'"+
							",valid_time = '"+DateTools.dtsWithTime(new Date())+"'"+
							",valid_detail = "+valid_detail+						
							",update_score= "+((vo.tmp_update_score != null && !"".equals(vo.tmp_update_score) )?vo.tmp_update_score:"NULL")+
							",update_user='" +user_id+"'" +
							",update_time ='"+DateTools.dtsWithTime(new Date())+"'"+
							" where id = " +v_id;
					}
					
					updateData(conn, sql);
				}
				else if((vo.detail_type == 2 && vo.c_id == 0))//用父数据,赋值到tmp_update_score
				{
					valid = vo.tmp_valid;
					tmp_update_score = vo.tmp_update_score;
					String sql = "update KPI_VALUE " +
						"set " +
						"valid = "+valid+
						",valid_user = '"+user_id+"'"+
						",valid_time = '"+DateTools.dtsWithTime(new Date())+"'"+
						",valid_detail = "+valid_detail+						
						",update_score= "+((vo.tmp_update_score != null && !"".equals(vo.tmp_update_score) )?vo.tmp_update_score:"NULL")+
						",update_user='" +user_id+"'" +
						",update_time ='"+DateTools.dtsWithTime(new Date())+"'"+
						" where year = " +vo.year+
						"	and month = "+vo.month+
						"	and kpi_id = "+vo.kpi_id+
						"	and kaohe_id = "+vo.kaohe_id;
					updateData(conn, sql);
				}
				else if((vo.detail_type == 2 && vo.c_id != 0))//用子数据替换父数据
				{
//					System.out.println(year+"年"+month+"月   审核：    valid="+vo.valid+"  tmp_valid="+vo.tmp_valid+"  score="+vo.score+"   update_score:"+vo.update_score+"   tmp_update_score:"+vo.tmp_update_score);
					
					String sql ;
					if(tmp_update_score == "")//没有临时数据，不需要修改tmp_update_score
					{
						sql = "update KPI_VALUE " +
							"set " +
							"valid = "+valid+
							",valid_user = '"+user_id+"'"+
							",valid_time = '"+DateTools.dtsWithTime(new Date())+"'"+
							",valid_detail = "+valid_detail+""+
							" where id = " +v_id+
							" and valid != 1";						
					}
					else
					{
						sql = "update KPI_VALUE " +
							"set " +
							"valid = "+valid+
							",valid_user = '"+user_id+"'"+
							",valid_time = '"+DateTools.dtsWithTime(new Date())+"'"+
							",valid_detail = "+valid_detail+						
							",update_score= "+((tmp_update_score != null && !"".equals(tmp_update_score) )?tmp_update_score:"NULL")+
							",update_user='" +user_id+"'" +
							",update_time ='"+DateTools.dtsWithTime(new Date())+"'"+
							" where id = " +v_id+
							" and valid != 1";	
					}
					
					updateData(conn, sql);
				}		
			}
			conn.commit();
			if(view_type == 0)
			{
//				return getKpiPointByGuikouItemForShow(0,kpi_id,year,month);
			}
			else
			{
//				return getKpiPointByGuikouKaoheForShow(0,kaohe_id,year,month);
			}
			
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			try{
				if(conn != null){
					conn.rollback();//回滚。
				}
			}catch(Exception ex2){
				ex2.printStackTrace();
			}
		}finally{
			try{
				if(conn != null){
					conn.setAutoCommit(true);
				}
			}catch(Exception ex2){
				ex2.printStackTrace();
			}
		}
		return l;
	}
	/*
	* just for savePoint method
	*/
	public List<KpiValueVo> checkPoint(List<KpiValueVo> l,String user_id,Connection conn) {
		try
		{//已经生成了父的一行。
			if(conn == null){
				conn = getConnect();				
			}
			String sql = "";
			int year =0;//输入的一组数据，应该是同一年，同一个月的。
			int month = 0;//输入的一组数据，应该是同一年，同一个月的。
			int kpi_id = 0;
			int kaohe_id = 0;
			int guikou_id = 0;
			BigDecimal pScore = new BigDecimal(0);
			String tmpGuikou_ids = "";
			for(int i = l.size()-1; i >=0 ; i--)
			{
				KpiValueVo vo = l.get(i);				
				
				String tmp_value = vo.tmp_value;
				if(tmp_value == null || "".equals(tmp_value))
				{
					tmp_value = "0";
				}
				String c_tmp_value = vo.c_tmp_value;
				if(c_tmp_value == null || "".equals(c_tmp_value))
				{
					c_tmp_value = "0";
				}
				if(year == 0){year = vo.year;}
				if(month == 0){month = vo.month;}
				
				String formula = vo.formula;
				String c_formula = vo.c_formula;
				int detail_type = vo.detail_type;
				int c_id = vo.c_id;
				
				if(detail_type == 2 && c_id != 0)//明细子，且有值
				{
					String replacedFormula = c_formula.replaceAll("X", c_tmp_value);
					if(replacedFormula.indexOf("/0.00")!=-1){
						System.out.println(">>>>>>>>>>>");
					}
					sql = "(select "+replacedFormula+")";
					
					ResultSet rs = queryData(conn,sql);
					String resultS = "";
					if(rs.next()){
						resultS = rs.getBigDecimal(1).setScale(4, BigDecimal.ROUND_HALF_UP).toString();
						pScore = pScore.add(rs.getBigDecimal(1).setScale(4, BigDecimal.ROUND_HALF_UP));
						tmpGuikou_ids += vo.guikou_id+":";
					}
					vo.c_score = resultS;
				}
				else if(detail_type == 2 && c_id  == 0)////明细，父类，无值
				{
					if(kpi_id == vo.kpi_id)
					{
						BigDecimal score = new BigDecimal(vo.point).add(pScore);
						{	
							String guikouIds = getSqlGuikouId(tmpGuikou_ids);
							String sql2 = "select sum(score) from (" +
							"	select * from KPI_DETAIL_VS_ORGANIZATION where kaohe_id = "+kaohe_id+" and parent_id = "+kpi_id+
							"	and guikou_id not in ("+guikouIds+")" +
							"	) as a,KPI_DETAIL_VALUE as d" +
							"	where a.guikou_id = d.guikou_id" +
							"	and a.kaohe_id = d.kaohe_id" +
							"	and a.kpi_detail_id = d.kpi_detail_id" +
							"	and d.year = "+year+" and d.month = "+month;
							ResultSet rs = queryData(conn, sql2);
							if(rs.next())
							{
								BigDecimal ts = rs.getBigDecimal(1);
								if(ts != null)
								{
									score = score.add(ts);
								}		
							}
						}
						
						if(vo.standard_type == 0)//设置上下限
						{
							if(score.compareTo(new BigDecimal(DataFormater.valueToSQL0(vo.point))) > 0)
							{
								vo.score = vo.point;
							}
							else if(score.compareTo(new BigDecimal(0)) < 0)
							{
								vo.score = "0";
							}
							else
							{
								vo.score = score.toString();
							}
						}
						else
						{
							vo.score = pScore.toString();
						}						
					}
					pScore = new BigDecimal(0);
					tmpGuikou_ids = "";
//					System.out.println("明细，父类，无值:"+vo.year+"   "+vo.month);
				}
				else 
				{
					if(detail_type == 3)
					{
						continue;
					}
					String replacedFormula = formula.replaceAll("X", tmp_value);
					ResultSet rs = queryData(conn,"select "+replacedFormula);
					String resultS = "";
					while(rs.next()){
						resultS = rs.getBigDecimal(1).setScale(4, BigDecimal.ROUND_HALF_UP).toString();
					}
//					if(new BigDecimal(point).compareTo(new BigDecimal(resultS)) < 0)//结果值大于固定分数
//					{
//						vo.score = point;
//					}
//					else if(new BigDecimal(resultS).compareTo(new BigDecimal(0)) < 0)//结果小于0
//					{
//						vo.score = "0";
//					}
//					else
//					{
						vo.score = resultS;
//					}
				}
				
				kpi_id = vo.kpi_id;
				kaohe_id = vo.kaohe_id;
				guikou_id = vo.guikou_id;
				
				
				vo.tmp_value = tmp_value;
				vo.c_tmp_value = c_tmp_value;
				l.set(i, vo);
			}
			
			//检查父子的数据，用子的分数替换父的分数。
//			l = calParentScore(l);
			return l;
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		return l;
	}
	
/**
	获取非本科室考核的其他子的明细扣分数据。（明细用的是扣分）	
*/	
	private BigDecimal getAllOtherChildsScore(int year,int month,int kpi_id,int kaohe_id,int guikou_id,Connection conn)
	{
		BigDecimal rtB = new BigDecimal(0); 
		try
		{
			String sql = "select sum(score) from (" +
			"	select * from KPI_DETAIL_VS_ORGANIZATION where kaohe_id = "+kaohe_id+" and parent_id = "+kpi_id+" and guikou_id != "+guikou_id +
			"	) as a,KPI_DETAIL_VALUE as d" +
			"	where a.guikou_id = d.guikou_id" +
			"	and a.kaohe_id = d.kaohe_id" +
			"	and a.kpi_detail_id = d.kpi_detail_id" +
			"	and d.year = "+year+" and d.month = "+month;
			ResultSet rs = queryData(conn, sql);
			if(rs.next())
			{
				BigDecimal score = rs.getBigDecimal(1);
				if(score != null)
				{
					rtB = rtB.add(score);
				}		
			}
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		return rtB.setScale(3, BigDecimal.ROUND_HALF_UP);
		
	}
//	private void updateOtherGuikouScore(int year,int month,int kpi_id,int kaohe_id,int guikou_id,String score,Connection conn)
//	{
//		try
//		{
//			String sql = "update KPI_VALUE set score = " +score+
//					"	where year = "+year +" and month = "+month+
//					"	and kpi_id = "+kpi_id+
//					"	and kaohe_id = "+kaohe_id;
//			updateData(conn, sql);
//		}
//		catch(Exception ex)
//		{
//			ex.printStackTrace();
//		}
//	}
	public List<OrganizationVo> getKaoheByGuikou(String guikou_id) 
	{
		try{
			Connection conn = getConnect();
			List<OrganizationVo> rtList = new ArrayList();
			String sql = "";
			String replaceGuikouId = getSqlGuikouId(guikou_id);
			if(replaceGuikouId == null)
			{
				return rtList;
			}
			else if(replaceGuikouId.equals("0"))
			{
//				sql =  "select distinct kaohe_id," +
//				"  (select name from dict_duty_type where id =(" +
//				"  select duty_id from t_sys_stat_department where id = KPI_VS_ORGANIZATION.kaohe_id)) as duty_name," +
//				"  (select newCode from  t_sys_stat_department where id = KPI_VS_ORGANIZATION.kaohe_id) as newCode," +
//				"  (select name from t_sys_stat_department where id = KPI_VS_ORGANIZATION.kaohe_id) as name " +				
//				"  from KPI_VS_ORGANIZATION" +
//				"  order by name";
				sql = "select distinct kaohe_id, "+
				"   (select name from dict_duty_type where id =( "+
				"   select duty_id from t_sys_stat_department where id = p.kaohe_id)) as duty_name,  "+
				"  (select newCode from  t_sys_stat_department where id = p.kaohe_id) as newCode,  "+
				"  (select name from t_sys_stat_department where id = p.kaohe_id) as name  "+
				"  from"+
				"  ("+
				"  select kaohe_id from KPI_VS_ORGANIZATION "+
				"  union all "+
				"  select kaohe_id from KPI_DETAIL_VS_ORGANIZATION "+
				"  ) as p"+
				"  order by name";
			}
			else
			{				
//				sql =  "select distinct kaohe_id," +
//				"  (select name from dict_duty_type where id =(" +
//				"  select duty_id from t_sys_stat_department where id = KPI_VS_ORGANIZATION.kaohe_id)) as duty_name," +
//				"  (select newCode from  t_sys_stat_department where id = KPI_VS_ORGANIZATION.kaohe_id) as newCode," +
//				"  (select name from t_sys_stat_department where id = KPI_VS_ORGANIZATION.kaohe_id) as name " +
//				"  from KPI_VS_ORGANIZATION" +
//				"  where guikou_id in( " +replaceGuikouId+")"+
//				"  order by name";
				sql = "select distinct kaohe_id, "+
				"   (select name from dict_duty_type where id =( "+
				"   select duty_id from t_sys_stat_department where id = p.kaohe_id)) as duty_name,  "+
				"  (select newCode from  t_sys_stat_department where id = p.kaohe_id) as newCode,  "+
				"  (select name from t_sys_stat_department where id = p.kaohe_id) as name  "+
				"  from"+
				"  ("+
				"  select kaohe_id from KPI_VS_ORGANIZATION where guikou_id in( " +replaceGuikouId+")"+
				"  union all "+
				"  select kaohe_id from KPI_DETAIL_VS_ORGANIZATION where guikou_id in( " +replaceGuikouId+")"+
				"  ) as p"+
				"  order by name";

			}
			
			
			
			ResultSet rs = queryData(conn,sql);
			
			OrganizationVo vo ;
			while(rs.next()){
				vo = new OrganizationVo();
				vo.id = rs.getInt("kaohe_id");
				vo.name = rs.getString("name");
				vo.duty_name = rs.getString("duty_name");
				vo.newCode = rs.getString("newCode");
				vo.full_name = vo.duty_name+"-"+vo.name;
				rtList.add(vo);
			}
			freeConnection(conn);
			return rtList;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	
	/**
	 * 把一个List里面明细的score，update_score汇总，加到父数据的对应属性
	 * 将父、子的score都设置成全体子c_score的合。
	 * **/
	private List<KpiValueVo> calParentScore(List<KpiValueVo> l,Connection conn)
	{		
		int year = 0;
		int month = 0;
		for(int i = 0;i<l.size();i++)
		{
			KpiValueVo vo = l.get(i);
			if(year == 0)
			{
				year = vo.year;
			}
			if(month == 0)
			{
				month = vo.month;
			}
			if(vo.detail_type == 2)//明细
			{
				BigDecimal score = new BigDecimal(vo.point);//父的总分
//				BigDecimal update_score = new BigDecimal(0);
				//获得归口科室是自己的全部明细的分数合。
				for(KpiValueVo vo2:l)
				{
					//获得归口科室是自己的全部明细的分数合。
					if(vo2.kaohe_id == vo.kaohe_id
							&& vo2.guikou_id == vo.guikou_id
							&& vo2.kpi_id == vo.kpi_id
						)
					{
						//父的总分，加上每个明细的扣分，得到最终的父的得分
						score = score.add(new BigDecimal(DataFormater.valueToSQL0(vo2.c_score)));
//						update_score = update_score.add(new BigDecimal(DataFormater.valueToSQL0(vo2.c_update_score)));
					}
				}
				//获得归口科室不是自己的全部明细的分数合。
				BigDecimal allOtherChildsScore = getAllOtherChildsScore(year,month,vo.kpi_id,vo.kaohe_id,vo.guikou_id,conn);//明细用的是扣分
				score = score.add(allOtherChildsScore);
				//应在此处处理基本项目和单项奖惩的上下限问题（单项奖惩是无上下限的；基本项目，有上限和下限（打架无下限））
				if(vo.standard_type == 0)//设置上下限
				{
					if(score.compareTo(new BigDecimal(DataFormater.valueToSQL0(vo.point))) > 0)
					{
						vo.score = vo.point;
					}
					else if(score.compareTo(new BigDecimal(0)) < 0)
					{
						vo.score = "0";
					}
					else
					{
						vo.score = score.toString();
					}
				}
				else
				{
					vo.score = score.toString();
				}
			}
		}
		return l;
	}
	private List<KpiValueVo> calParentScore(List<KpiValueVo> l)
	{		
		Connection conn = getConnect();
		int year = 0;
		int month = 0;
		for(int i = 0;i<l.size();i++)
		{
			KpiValueVo vo = l.get(i);
			if(year == 0)
			{
				year = vo.year;
			}
			if(month == 0)
			{
				month = vo.month;
			}
			if(vo.detail_type == 2)//明细
			{
				BigDecimal score = new BigDecimal(vo.point);//父的总分
//				BigDecimal update_score = new BigDecimal(0);
				//获得归口科室是自己的全部明细的分数合。
				for(KpiValueVo vo2:l)
				{
					//获得归口科室是自己的全部明细的分数合。
					if(vo2.kaohe_id == vo.kaohe_id
							&& vo2.guikou_id == vo.guikou_id
							&& vo2.kpi_id == vo.kpi_id
						)
					{
						//父的总分，加上每个明细的扣分，得到最终的父的得分
						score = score.add(new BigDecimal(DataFormater.valueToSQL0(vo2.c_score)));
//						update_score = update_score.add(new BigDecimal(DataFormater.valueToSQL0(vo2.c_update_score)));
					}
				}
				//获得归口科室不是自己的全部明细的分数合。
				BigDecimal allOtherChildsScore = getAllOtherChildsScore(year,month,vo.kpi_id,vo.kaohe_id,vo.guikou_id,conn);//明细用的是扣分
				score = score.add(allOtherChildsScore);
				//应在此处处理基本项目和单项奖惩的上下限问题（单项奖惩是无上下限的；基本项目，有上限和下限（打架无下限））
				if(vo.standard_type == 0)//设置上下限
				{
					if(score.compareTo(new BigDecimal(DataFormater.valueToSQL0(vo.point))) > 0)
					{
						vo.score = vo.point;
					}
					else if(score.compareTo(new BigDecimal(0)) < 0)
					{
						vo.score = "0";
					}
					else
					{
						vo.score = score.toString();
					}
				}
				else
				{
					vo.score = score.toString();
				}				
			}
		}
		freeConnection(conn);
		return l;
	}


	public void updateKpiDictItem(KpiDictItemVo vo) {
		try{
			Connection conn = getConnect();			
			
			if(vo.detail_type == 2)//有明细要删除
			{
				String sql = "delete DICT_KPI_ITEM_DETAIL where parent_id = "+vo.id;
				updateData(conn, sql);
			}
			String sql = "delete DICT_KPI_ITEM where id = "+vo.id;
			updateData(conn, sql);
//			vo.id
			freeConnection(conn);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
//	private String getScore(Connection conn,String formula,int standard_type,int detail_type,String value,List<String> valueList)
//	{
//		try
//		{
//			if(detail_type != 2)
//			{	
//				ResultSet rs = queryData(conn,"select "+formula.replaceAll("X", value));
//				String resultS = "";
//				while(rs.next()){
//					resultS = rs.getBigDecimal(1).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
//					break;
//				}
//				return resultS;
//			}
//			else//明细
//			{
//				if(no_double == 1){//一次扣分
//					sql += "(select case when"+replacedFormula+" < 0 then 0 else "+replacedFormula+" end)";
//				}else{//累加扣分
//					sql += "(select "+replacedFormula+")";
//				}
//			}
//		}
//		catch(Exception ex)
//		{
//			ex.printStackTrace();
//		}
//		return "";
//	}

	private int isOverDue(int y,int m)
	{
		int date = new Integer(new MsgReader().getPt("OVERDUE_DATE"));
		if(date == 0)
		{
			return 0;
		}
		Calendar ca1 = Calendar.getInstance();
		ca1.clear();
		ca1.set(y, m-1, date);
		
		Calendar caNow = Calendar.getInstance();
//		System.out.println(ca1.getTime()+"    "+caNow.getTime());
		if(caNow.before(ca1))
		{
			return 0;
		}
		else
		{
			return 1;
		}
		
	}
	
	public List<KpiValueVo> getMyKpiWithoutDetail(String code,Integer guikou_id)
	{
		try{
			Connection conn = getConnect();
			List<KpiValueVo> rtList = new ArrayList();
			String sql =  "select id as pid,null as cid,item_name,display_name as p_display_name,null as c_display_name,description,point,frequence" +
					"	from DICT_KPI_ITEM" +
					"	where id in(" +
					"	select distinct kpi_id from KPI_VS_ORGANIZATION" +
					"	where guikou_id = "+guikou_id+
					"	)" +
					"	union all" +
					"	select parent_id as pid,id as cid,item_name," +
					"	(select di.display_name from DICT_KPI_ITEM as di where parent_id = di.id) as p_display_name," +
					"	display_name  as c_display_name," +
					"	(select di.description from DICT_KPI_ITEM as di where parent_id = di.id) as description,	" +
					"	(select di.point from DICT_KPI_ITEM as di where parent_id = di.id) as point," +
					"	(select di.frequence from DICT_KPI_ITEM as di where parent_id = di.id) as frequence" +
					"	from DICT_KPI_ITEM_DETAIL where id in" +
					"	(" +
					"	select distinct kpi_detail_id from KPI_DETAIL_VS_ORGANIZATION" +
					"	where guikou_id = "+guikou_id+
					"	)" +
					"	order by pid,cid" ;				
			ResultSet rs = queryData(conn,sql);
			
			KpiValueVo vo ;
			while(rs.next()){
				vo = new KpiValueVo();
				
				vo.c_id = rs.getInt("cid");
				vo.c_parent_id = rs.getInt("pid");
				vo.c_item_name = rs.getString("item_name");
				vo.c_display_name = rs.getString("c_display_name");
				vo.display_name = rs.getString("p_display_name");
				vo.description = rs.getString("description");
				vo.point = rs.getString("point");
				vo.frequence = rs.getString("frequence");
				
//				vo.description = rs.getString("description");
//				vo.frequence = rs.getString("frequence");
//				vo.formula = rs.getString("formula");
//				vo.point = rs.getBigDecimal("point") != null?rs.getBigDecimal("point").toString():"";
//				vo.detail_type = rs.getInt("detail_type");
				
				rtList.add(vo);
			}
			return rtList;
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		return null;
	}

	public List<KpiValueVo> getMyAssessment(String guikou_ids,String year,String month,
			int kaohe_id,String item_name,String status){
		try{
			Connection conn = getConnect();
			
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}


	public List<KpiValueVo> validAll(List<KpiValueVo> l, String user_id,
			int view_type) {
		Connection conn = getConnect();	
		try
		{			
			conn.setAutoCommit(false);
			String tmp_update_score = "";
			int valid = 0;//如果是明细，一定是父先更新，然后子再用。
			int year = 0;
			int month = 0;
			for(KpiValueVo vo :l)
			{
				if(vo.valid == 1)//已经验证，无需更改
				{
					continue;
				}
				if(vo.v_id == 0)//尚未提交数据，无需更改
				{
					continue;
				}
				
				vo.tmp_valid = 1;//其余都验证通过
				int v_id = vo.v_id;
				year = vo.year;
				month = vo.month;	
				
				
				String valid_detail = (vo.valid_detail!=null && !"".equals(vo.valid_detail))?("'"+vo.valid_detail.replaceAll("'", "''")+"'"):"NULL";
				
				if(vo.detail_type != 2)//非明细数据，都当作父考虑
				{
//					System.out.println(year+"年"+month+"月   审核：    valid="+vo.valid+"  tmp_valid="+vo.tmp_valid+"  score="+vo.score+"   update_score:"+vo.update_score+"   tmp_update_score:"+vo.tmp_update_score);
					valid = vo.tmp_valid;
					String sql ;
					if(vo.tmp_update_score == "")//没有临时数据，不需要修改tmp_update_score
					{
						sql = "update KPI_VALUE " +
							"set " +
							"valid = "+valid+
							",valid_user = '"+user_id+"'"+
							",valid_time = '"+DateTools.dtsWithTime(new Date())+"'"+
							",valid_detail = "+valid_detail+""+
							" where id = " +v_id;						
					}
					else
					{
						sql = "update KPI_VALUE " +
							"set " +
							"valid = "+valid+
							",valid_user = '"+user_id+"'"+
							",valid_time = '"+DateTools.dtsWithTime(new Date())+"'"+
							",valid_detail = "+valid_detail+						
							",update_score= "+((vo.tmp_update_score != null && !"".equals(vo.tmp_update_score) )?vo.tmp_update_score:"NULL")+
							",update_user='" +user_id+"'" +
							",update_time ='"+DateTools.dtsWithTime(new Date())+"'"+
							" where id = " +v_id;
					}
					
					updateData(conn, sql);
				}
				else if((vo.detail_type == 2 && vo.c_id == 0))//用父数据,赋值到tmp_update_score
				{					
					valid = vo.tmp_valid;
					tmp_update_score = vo.tmp_update_score;
					String sql = "update KPI_VALUE " +
						"set " +
						"valid = "+valid+
						",valid_user = '"+user_id+"'"+
						",valid_time = '"+DateTools.dtsWithTime(new Date())+"'"+
						",valid_detail = "+valid_detail+						
						",update_score= "+((vo.tmp_update_score != null && !"".equals(vo.tmp_update_score) )?vo.tmp_update_score:"NULL")+
						",update_user='" +user_id+"'" +
						",update_time ='"+DateTools.dtsWithTime(new Date())+"'"+
						" where year = " +vo.year+
						"	and month = "+vo.month+
						"	and kpi_id = "+vo.kpi_id+
						"	and kaohe_id = "+vo.kaohe_id;
					updateData(conn, sql);
				}
				else if((vo.detail_type == 2 && vo.c_id != 0))//用子数据替换父数据
				{
//					System.out.println(year+"年"+month+"月   审核：    valid="+vo.valid+"  tmp_valid="+vo.tmp_valid+"  score="+vo.score+"   update_score:"+vo.update_score+"   tmp_update_score:"+vo.tmp_update_score);
					
					String sql ;
					if(tmp_update_score == "")//没有临时数据，不需要修改tmp_update_score
					{
						sql = "update KPI_VALUE " +
							"set " +
							"valid = "+valid+
							",valid_user = '"+user_id+"'"+
							",valid_time = '"+DateTools.dtsWithTime(new Date())+"'"+
							",valid_detail = "+valid_detail+""+
							" where id = " +v_id+
							" and valid != 1";						
					}
					else
					{
						sql = "update KPI_VALUE " +
							"set " +
							"valid = "+valid+
							",valid_user = '"+user_id+"'"+
							",valid_time = '"+DateTools.dtsWithTime(new Date())+"'"+
							",valid_detail = "+valid_detail+						
							",update_score= "+((tmp_update_score != null && !"".equals(tmp_update_score) )?tmp_update_score:"NULL")+
							",update_user='" +user_id+"'" +
							",update_time ='"+DateTools.dtsWithTime(new Date())+"'"+
							" where id = " +v_id+
							" and valid != 1";	
					}
					
					updateData(conn, sql);
				}		
			}
			conn.commit();
			
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			try{
				if(conn != null){
					conn.rollback();//回滚。
				}
			}catch(Exception ex2){
				ex2.printStackTrace();
			}
		}finally{
			try{
				if(conn != null){
					conn.setAutoCommit(true);
				}
			}catch(Exception ex2){
				ex2.printStackTrace();
			}
		}
		return l;
	
	}
	
	
	public List<KpiDictItemVo> getMyKaoheKpiItem(String user_name) {
		try{
			Connection conn = getConnect();
			String sql = "select "+DICT_KPI_ITEM_COLUMNS+" from DICT_KPI_ITEM where id in" +
				" 	(" +
				" 	select distinct parent_id as kpi_id from KPI_DETAIL_VS_ORGANIZATION" +
				"	where guikou_id in" +
				"	(select d.id from t_sys_users_vs_department as uvd" +
				"		left join t_sys_stat_department as d" +
				"		on uvd.organization_id = d.id" +
				"		where code = '"+user_name+"')"+
				"	union	" +
				"	select distinct kpi_id from KPI_VS_ORGANIZATION where guikou_id  in" +
				"	(select d.id from t_sys_users_vs_department as uvd" +
				"		left join t_sys_stat_department as d" +
				"		on uvd.organization_id = d.id" +
				"		where code = '"+user_name+"')"+
				" ) order by id";
			

			ResultSet rs = queryData(conn,sql);
			List<KpiDictItemVo> rtList = new ArrayList();
			KpiDictItemVo vo ;
			while(rs.next()){
//				"id,item_name,display_name,description,frequence,"+//id，名称，显示名称，描述，频次（0001，0002，0003，0004）
//				"point,target_value,"+//分数，标准值
//				"target_type,"+//1越高越好、2越低越好
//				"standard_type,"+//0标准、1奖惩、2一票
//				"detail_type,"+//0标准、1区间、2明细、3录入
//				"different_scale,"+//每偏离？
//				"add_type,"+//偏离的话，是增加还是减少  1增2减0等
//				"scale_point,"+//偏离一个单位，扣/加的分数
//				"formula,"+//整理后的公式
//				"default_value," +//默认值
//				"section_detail,danwei
				vo = new KpiDictItemVo();
				vo.id = rs.getInt(1);
				vo.item_name = rs.getString(2);
				vo.display_name = rs.getString(3);
				vo.description = rs.getString(4);
				vo.frequence = rs.getString(5);
				vo.point = DataFormater.numberToString(rs.getBigDecimal(6));
				vo.target_value = DataFormater.numberToString(rs.getBigDecimal(7));
				vo.target_type = rs.getInt(8);
				vo.standard_type = rs.getInt(9);
				vo.detail_type = rs.getInt(10);
				vo.different_scale = DataFormater.numberToString(rs.getBigDecimal(11));
				vo.add_type = rs.getInt(12);
				vo.scale_point = DataFormater.numberToString(rs.getBigDecimal(13));
				vo.formula = rs.getString(14);
				vo.default_value = DataFormater.numberToString(rs.getBigDecimal(15));
				vo.section_detail = rs.getString(16);
				vo.danwei = rs.getInt(17);
				rtList.add(vo);
			}
			freeConnection(conn);
			return rtList;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public KpiValueVo valueToScore(KpiValueVo vo,Connection conn) {
		try
		{//已经生成了父的一行。
			if(conn == null){
				conn = getConnect();				
			}
			String sql = "";
			BigDecimal pScore = new BigDecimal(0);
			
			String formula = vo.formula;
			String c_formula = vo.c_formula;
			int detail_type = vo.detail_type;
			
			if(detail_type == 2 && vo.c_id != 0){//明细子，且有值
				String c_tmp_value = vo.c_tmp_value;
				if(c_tmp_value == null || "".equals(c_tmp_value))
				{
					c_tmp_value = "0";
				}
				String replacedFormula = c_formula.replaceAll("X", c_tmp_value);
				sql = "(select "+replacedFormula+")";
				
				ResultSet rs = queryData(conn,sql);
				String resultS = "";
				if(rs.next()){
					resultS = rs.getBigDecimal(1).setScale(4, BigDecimal.ROUND_HALF_UP).toString();
					pScore = pScore.add(rs.getBigDecimal(1).setScale(4, BigDecimal.ROUND_HALF_UP));
				}
				vo.c_score = resultS;
				return vo;
			}else{
				String tmp_value = vo.tmp_value;
				if(tmp_value == null || "".equals(tmp_value))
				{
					tmp_value = "0";
				}
				if(detail_type == 3)
				{
					return vo;
				}
				String replacedFormula = formula.replaceAll("X", tmp_value);
				ResultSet rs = queryData(conn,"select "+replacedFormula);
				String resultS = "";
				while(rs.next()){
					resultS = rs.getBigDecimal(1).setScale(4, BigDecimal.ROUND_HALF_UP).toString();
				}
				vo.score = resultS;
				return vo;
			}
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		return vo;
	}
}
